views:

47

answers:

2

How can I use a variable in getElementById

html:

   <td><a href="#" onclick='selectEditActivity("id3319201010153333");'>Click</a></td>

javascript:

function selectEditActivity(pass_id){  
  // this works
  alert(pass_id)
  // this works;
  var A = document.getElementById("id33192010101533333").getAttribute("seq");
  alert(A);
  // but this does not when I use the variable

  var B = document.getElementById(pass_id).getAttribute("seq");
  alert(B);
+2  A: 

You're missing a 3 in your onclick handler. It should be:

onclick='selectEditActivity("id33192010101533333");'

Instead of:

onclick='selectEditActivity("id3319201010153333");'
Frédéric Hamidi
+1  A: 

id33192010101533333 is not the same thing as id3319201010153333. One of them has an extra 3 at the end.

Use the same ID in your variable and it'll work fine.

VoteyDisciple