0

I have the problem with selecting strings:

var idToChange = $(obj).attr('id').replace('#', '');

window.location = $("'li .' + idToChange + ' a'").attr('href');

This doesn't reload the page but when I do alert('li .' + idToChange + ' a'); I get the correct value, and when I use firebug console to get ('li .' + idToChange + ' a').attr('href'); I also get correct string. I think I am messing something up with quotes during selecting. Can anyone help?

bobek
  • 7,721
  • 8
  • 33
  • 72

1 Answers1

4

Remove the double quotes, you can already see with the syntax highlighter that everything inside of $(...) is red (meaning the it is treated as string):

$("'li .' + idToChange + ' a'")

vs

$('li .' + idToChange + ' a')
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
  • 2
    I like it when the syntax highlighting in their question shows the error. – mrtsherman Nov 07 '11 at 00:11
  • I tried that but it doesn't work. And I tried doing alert on it , and I get undefined. – bobek Nov 07 '11 at 00:13
  • @bibek: Maybe it does not select the correct element... but if `$('li .' + idToChange + ' a').attr('href');` works for you in the console, then it should also in the code. – Felix Kling Nov 07 '11 at 00:16
  • That's what I thought but when I do `alert($(obj).attr('id').replace('#', ''));` I get correct value, and then when I use that value in firebug console it finds it and it works nicely. – bobek Nov 07 '11 at 00:20
  • @bobek: Then you have to create a http://jsfiddle.net/ example, there is nothing more to say otherwise. – Felix Kling Nov 07 '11 at 00:22
  • I removed 'li' from 'li .' and now it works :) Thanks for your help! – bobek Nov 07 '11 at 00:35