JSidiot JSidiot - 6 months ago 10
Javascript Question

How to pass data from onclick button to variable

I'm doing my first Jquery related website. I'm now stucked: how to pass data from button to variable ?

Script:

echo nl2br("<br><h1>".$row2['header']."</h1><br><h4 $style>".htmlentities($row2['text'])."</h4><hr><br>
<input class="btn btn-default" type="submit" data=".$row2['ID']." value="submit" onclick="return passID()">");


jQuery:

function passid()
{
var ID=??????
}

Answer

Add the row ID to the passID function in the inline onclick event.

<input class="btn btn-default" type="submit" data=".$row2['ID']." value="submit" onclick="return passID(".$row2['ID'].")">"

Then change your function to accept an ID variable

 function passid(rowId){
  var ID=rowId;    
 }