Skip to content
Advertisement

not able to access the dynamic id in a table row

var id =123; 
<td>
  <a href="objectives.html" onclick="location.href=this.href+'?id='+ id;return false;"> 
    <i class="fas fa-angle-right"></i> 
  </a>
</td>

Inside a table <td> I have put this code and not able to fetch the id inside the td. Id can not be fetched and not able to pass the id in href.

Advertisement

Answer

Code works if you rename the id to myId. There is likely a window.id that is shadowing your variable

var id=123;
var myId=123;
<td>
  <a href="https://google.com/search" 
  onclick="location.href=this.href+'?q='+ myId;return false;"> 
    <i class="fas fa-angle-right"></i> Click
  </a>
</td>
<hr/>
Test:

<td>
  <a href="" 
  onclick="console.log('id',id,'myId',myId);return false;"> 
    <i class="fas fa-angle-right"></i> Click
  </a>
</td>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement