0

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!!!

1
  • I'm using blade and new to this template, how can I pass variables between HTML and JS? Commented Feb 12, 2017 at 23:41

1 Answer 1

2

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.

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

3 Comments

The return value of getElementByClassName is not an element object, so that won't work.
hmm, then we can use querySelector('.icheck'), this will return an element
but I have multiple radio buttons, they are siblings and using same class, what kind of selector should I use? Thank you! @Mohd Asim Suhail

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.