...

View Full Version : how to add variable in javascript function



o0O0o.o0O0o
06-03-2009, 06:31 AM
I am using Jquery but the question belongs to simple JS




$("#anyID > tbody")

Now this works fine where # comes before any ID

The problem is if i have anyID in JS variable like


var1= "#anyID";

How can i insert that in above function


$(var1 + "> tbody"); is not working

rnd me
06-03-2009, 07:04 AM
the two strings are different...

does jQuery need the space between "D" and ">" ?

Deacon Frost
06-03-2009, 09:02 AM
Did you try to print the variable, to be sure it's storing properly?

Did you try storing the other text into a variable?

For instance:



var1 = "#anyID"
var2 = "tbody"
$(var1 > var2)


Or maybe just try:



$(var1 > "tbody")


Technically, that's a string, and since it's a string, it cannot have a numerical value.

So you'll have to associate a numerical value to it.

so instead of being just tbody, it'd be:



tbody = 1


And then you'd simply remove the quotes, and plus sign, and it should work.



the two strings are different...

does jQuery need the space between "D" and ">" ?

True, but even if they are different languages, a numerical value is a numerical value is a numerical value. As long as you call the variable properly inside of the code you want to compare in, it doesn't matter if it's php, js, asp, or whatever.

It probably doesn't need the space, I don't know too much about jQuery, but I'd assume that the context above is neater than cramping the code entirely together, making it more difficult to troubleshoot.

Actinia
06-03-2009, 03:43 PM
I would guess that an expression like
(var1 > var2) or
(var1 > "tbody") are seen by Javascript as comparisons, and would yield a boolean value. My guess is that you should be using string concatenation such as:
$(var1 + " > tbody") or
$(var1 + " > " var2).

As suggested, you should try inserting an alert to confirm what you thing is the right expression, or better still, use firebug's console.log.

John

o0O0o.o0O0o
06-04-2009, 06:52 AM
thanks guys its working now



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum