views:

103

answers:

3

For some reason, this works:

var oldText = $("#tayke li:eq(" + theIndex + ")" ).text();

But this doesn't:

var tayke_li =  "#tayke li:eq(" + theIndex + ")"
var oldTest = $( tayke_li ).text();

Note: theIndex is an integer.

A: 

Does it make any difference if you put the semi-colon at the end of then line:

var tayke_li =  "#tayke li:eq(" + theIndex + ")"; //<---

I've set up a simple example, and it works fine:

alert($("#tab1").length);
var s = "#tab" + String(1); //alerts "1"
alert(s);  //alerts "#tab1"
alert($(s).length); //alerts 1

Also, try an explicit cast of theIndex to a string using String(). Have you double-checked what is stored in tayke_li

Semicolons Required?

http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript

http://stackoverflow.com/questions/1031718/what-is-the-consequence-of-this-bit-of-javascript

http://stackoverflow.com/questions/537632/should-i-use-semi-colons-in-javascript-closed

http://www.webmasterworld.com/forum91/521.htm

James Wiseman
ahhh, just a typo. makes no difference though
Phil
just tried var tayke_li = "#tayke li:eq(" + String(theIndex) + ")"; no luck
Phil
try `var tayke_li = "#tayke li:eq(" + theIndex.toString() + ")"`
Darmen
what does alert("#tayke li:eq(" + theIndex + ")"); give you?
James Wiseman
Darmen, still not working. James, it gives me #tayke li:eq(3) (when I click on the third list item)
Phil
@James Wiseman: Neither of your suggestions are issues that would cause problems in JavaScript. Semi-colons aren't required, and integers that are concatenated with strings are automatically cast to a string.
Andy E
@Andy E. You are incorrect. Granted that they would have not have caused problems in the ABOVE JavaScript, but to state that they are not needed entirely is erroneous. There are situations where you HAVE to use a semi-colon, and where you HAVE to explicit cast. Much in the same way that there situations where the opening brace { MUST appear on the same line as the defining clause.
James Wiseman
I didn't say they weren't needed, I said they weren't required. Requirement is a definition of the spec, need is a definition of the developer, javascript doesn't *require* semi-colons at the end of a statement, but a developer may *need* to often. And I was commenting and voting in the context of the question and your answer to the question, *not JavaScript in general*. AFAIK, there are no situations in JS that require you to cast to a string for string concatenation. Please don't take it personally, this is what the voting system is for.
Andy E
+1  A: 

It works both ways. I've redone it and it worked. Check the theIndex variable for changes and scope. Try replacing it with hardcoded 1

adding jQuery version info to the question, and a browser spec would be nice too.

naugtur
oldText != oldTest is the only explanation why this shouldn't not work IMHO ;) but this is not what I'd put as an answer.
naugtur
A: 

try

var oldText = $("#tayke li:eq("+parseInt(theIndex)+")").text();

or

var oldText = $("#tayke li").eq(theIndex).text();

dotty