1

Can any body please tell me the difference between

  1. $(".level3_td[data-levelid=" + 01 + "]") and
  2. $(".level3_td[data-levelid=01]")

I am dynamically generating $(".level3_td[data-levelid=" + 01 + "]") but it doesn't seem to find the item that I am trying to find. Then I tried to paste it in the console and found that it wasn't able to find the DOM object. After that I tried the 2nd one by hard coding $(".level3_td[data-levelid=01]") and it worked.

Can anybody please tell me what's the difference between both of these and how may I get the first one to work?

1
  • 9
    Surely 01 will result in levelid=1. Leading zeros are not a component of an integral number. Commented Sep 19, 2013 at 14:20

1 Answer 1

6

Your 01 is getting converted into a 1, dropping the 0. You'll need to tell Javascript that you want to treat the 01 as a string by wrapping it in quotes. Something like:

$(".level3_td[data-levelid=" + "01" + "]")

So in reality, your code is attempting to access $(".level3_td[data-levelid=1]") which most likely does not exist.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but in case when 01 is being generated by a variable, how may I get this to work?
@KamranAhmed There's no built in functionality, to the best of my knowledge, for zero padding out an integer like that. You're going to have to roll your own: jsfiddle.net/qQUfX

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.