Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

naming variabes

Status
Not open for further replies.

TurboSRT4

Programmer
Nov 23, 2009
47
US
hello I have this

var test06 = 'blahblahblahblah;

function test(mode){
alert(mode);
}

....onclick=javascript:test(test06);

how would i make it alert the value of the already declared variable as oposed to what is and assigning the literal text 'test06' to the alert box. i need to make the alert box declare blahblalahblah

this is far from what i am doing but figured this would be most simplest explained thank you
 
Code:
<script>
myVar06 = "blah";
myVar07 = "blah blah";
function test(strmode) {
	alert([COLOR=red]eval([/color]strmode[COLOR=red])[/color])
}
test([COLOR=red]"[/color]myVar07[COLOR=red]"[/color]);
</script>


--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
For starters, you may want to end your string with a quote otherwise you are likely getting errors.

Code:
var test06 = 'blahblahblahblah[red]'[/red];

Other than that, the alert should show the value of the passed variable as is.

So you should be getting the 'blahblahblah', unless your actual code has quotes around the variable name test06 in your function call.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Incidentally, instead of this:

Code:
alert(eval(strmode))

I would use this:

Code:
alert(window[strmode]);

I don't know if eval is that much slower using today's faster JS engines, but I'd certainly avoid it if I could.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Thanks for the suggestion, BillyRay, I'll try that next time...

I'm assuming window[x] is the same as eval(x) ???? And can be used anywhere I use eval???




--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
I'm assuming window[x] is the same as eval(x) ???? And can be used anywhere I use eval???

Not at all. eval will always evaluate the string passed to it. window[x] is the same as window.x or, in the case of accessing a global variable, simply x. You can read a more in depth explanation at the end of my post dated 23 Mar 10 5:25 in thread216-1596168.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top