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.

Phil
  • 137
  • 1
  • 10
  • 2
    You have `;` missed before line 2 or is this just a typo? – Darmen Amanbay Feb 12 '10 at 13:19
  • 2
    another typo in comparison to the first is oldText became oldTest (*x -> s*). If you are alerting that to see if it works it will fail.. – Gabriele Petrioli Feb 12 '10 at 13:22
  • 1
    When you say it doesn't work, is it just not selecting what you want or is the code throwing an error? – Adrian Lynch Feb 12 '10 at 13:26
  • Woah, Gaby, nice catch. I'm an idiot. I think that was it. It seems to be working now. EDIT: I'm new to Stack Overflow so I don't know if this is allowed, but if you make an official answer with that response, I'll mark it accepted. – Phil Feb 12 '10 at 13:33
  • Using a good IDE like WebStorm, you would have unused variables underlined and many other features. For professional javascript developers, it's really worth the cost. – Nicolas Zozol Oct 12 '13 at 09:58

3 Answers3

1

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
  • 16,827
  • 5
  • 70
  • 113
  • 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 Feb 12 '10 at 13:33
0

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?

Do you recommend using semicolons after every statement in JavaScript?

What is the consequence of this bit of javascript?

Should I use semicolons in JavaScript?

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

Community
  • 1
  • 1
James Wiseman
  • 29,946
  • 17
  • 95
  • 158
  • ahhh, just a typo. makes no difference though – Phil Feb 12 '10 at 13:18
  • just tried var tayke_li = "#tayke li:eq(" + String(theIndex) + ")"; no luck – Phil Feb 12 '10 at 13:20
  • try `var tayke_li = "#tayke li:eq(" + theIndex.toString() + ")"` – Darmen Amanbay Feb 12 '10 at 13:22
  • what does alert("#tayke li:eq(" + theIndex + ")"); give you? – James Wiseman Feb 12 '10 at 13:24
  • Darmen, still not working. James, it gives me #tayke li:eq(3) (when I click on the third list item) – Phil Feb 12 '10 at 13:26
  • @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 Feb 12 '10 at 13:27
  • @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 Feb 12 '10 at 13:39
  • 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 Feb 12 '10 at 13:59
0

try

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

or

var oldText = $("#tayke li").eq(theIndex).text();
Harry
  • 87,580
  • 25
  • 202
  • 214
dotty
  • 40,405
  • 66
  • 150
  • 195