Kamran Ahmed
Kamran Ahmed

Reputation: 12438

Executing dynamically generated Jquery code

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?

Upvotes: 1

Views: 107

Answers (1)

Julio
Julio

Reputation: 2290

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.

Upvotes: 6

Related Questions