0

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=??????
 }

3 Answers 3

2

$("#identify_this_btn").click(function() {
  
  var data = $(this).attr("data"); // GET THE DATA IN ATTR
  event.preventDefault();
  alert(data);
  console.log(data);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="btn btn-default" id="identify_this_btn" type="submit" data="this_is_some_data" value="submit">

Sign up to request clarification or add additional context in comments.

Comments

0

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;    
 }

4 Comments

Thanks a alot, this worked! Is there possible pass multible variables with this btw?
Yes, as many as required. As long as you have a function with the correct definition available.
It seems to be able only pass numbers with that(?) Is there way to pass text with this?
Echo your HTML with single quotes around the variable
0

Try this one.

On php:

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

On javascript:

function passId(id){ console.log(id); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.