Adamhumbug Posted July 15, 2020 Share Posted July 15, 2020 I have a function in JS that i would like to pass 2 variables to. One is an int and one is a string. I am struggling to get the string part right as it needs to be wrapped in quotes. My code is : $out.="<div onclick='newTransactionLine(".$r['id'].",".$r['direction'].")' class='transactionItem btn $class d-block mb-2' data-id='".$r['id']."' >".$r['item']."<br/>£".$r['price']."</div>"; which outputs: <div onclick="newTransactionLine(1,OUT)" class="transactionItem btn btn-primary d-block mb-2" data-id="1">Adult Membership (£85)<br>£85.00</div> When i click the button that gets made, i get a reference error on the word out. I have no doubt it is very simple but i just cannot get this to work for me. Quote Link to comment https://forums.phpfreaks.com/topic/311096-creating-onclick-js-func-from-php/ Share on other sites More sharing options...
kicken Posted July 15, 2020 Share Posted July 15, 2020 See how you have data-id='".$r['id']."' for your $r['id'] variable? Do the same thing with your direction variable. data-direction='".$r['direction']."' Then in your javascript code you can access these values via the dataset property on the element. onclick="newTransactionLine(this.dataset.id, this.dataset.direction)" Quote Link to comment https://forums.phpfreaks.com/topic/311096-creating-onclick-js-func-from-php/#findComment-1579649 Share on other sites More sharing options...
Adamhumbug Posted July 16, 2020 Author Share Posted July 16, 2020 18 hours ago, kicken said: See how you have data-id='".$r['id']."' for your $r['id'] variable? Do the same thing with your direction variable. data-direction='".$r['direction']."' Then in your javascript code you can access these values via the dataset property on the element. onclick="newTransactionLine(this.dataset.id, this.dataset.direction)" Bingo - thanks very much!! Quote Link to comment https://forums.phpfreaks.com/topic/311096-creating-onclick-js-func-from-php/#findComment-1579671 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.