Leo
Leo

Reputation: 11

Pass PHP variables from HTML to Javascript

I want to do something to 'input box' when the radio button is checked, so I'm thinking using JS to solve this, here's part of my HTML code:

<input type="radio" class="icheck" id='DELIVER_{{$shoppingcart->id}}' />

$shoppingcart->id is from database, so the ID is dynamic, so now I'm using JS like:

var deliverId = DELIVER_{{ $shoppingcart->id }};
if(document.getElementById(DELIVER_{{$shoppingcart->id}}).checked) {

    //do something

else if (...)
...

but the error is 'shoppingcart is not defined', how can I define it or solve the issue?

Thanks in advance!!!

Upvotes: 0

Views: 200

Answers (1)

Mohd Asim Suhail
Mohd Asim Suhail

Reputation: 2292

You can get the id on radio box using querySelectorAll document.querySelector('icheck').id and then iterating over all the available radio buttons

for(var i=0;i<document.querySelectorAll('.icheck').length;i++){
  var deliverId=document.querySelectorAll('.icheck')[i].id;
  if(document.getElementById(deliverId).checked) {

    //do something

  else if (...)
  ...
}

This will solve your problem.

Upvotes: 2

Related Questions