tjohnson_nb
tjohnson_nb

Reputation: 70

I can't seem to pass a value to javascript function

My code is like so

function show_file(itemid) {
  document.getElementById("itemid").innerHTML = "Testing";
}
<td id='$itemid' class='header-row'>
  MyText quote&nbsp;&nbsp;
  <button onclick='show_file($itemid)'>
    Button<i class='fa fa-angle-down'></i>
  </button>
</td>

But I get this error "Cannot set property 'innerHTML' of null" so it seems the value of $itemid is not getting passed?

Upvotes: 0

Views: 90

Answers (2)

antonyftp
antonyftp

Reputation: 70

That is because you gived a string in parameter to your getElementById function and not the itemid variable. Try this :

function show_file(itemid) {
  document.getElementById(itemid).innerHTML = "Testing";
}

Upvotes: 1

Kamen Kanev
Kamen Kanev

Reputation: 311

You should pass the actual function's parameter itemid not the string "itemid"

Change it from:

function show_file(itemid) {
  document.getElementById("itemid").innerHTML = "Testing";
}

To:

function show_file(itemid) {
  document.getElementById(itemid).innerHTML = "Testing";
}

Upvotes: 1

Related Questions