Question:
Setup checkbox for multiple switch on change event of Javascript

Several switch buttons for the form are written in JavaScript and Laravel. I want to save in the database.


my blade:

<form action="{{ route('admin.settings.post') }}" method="POST">


<label class="switch">

<input class="id-target" onchange="click_switch(this)" value="{{ $post }}" name="post" autocomplete="off" type="checkbox" @if($post == 'on') checked="" @endif>

<span class="slider round"></span>

</label>


<label class="switch">

<input class="id-target" onchange="click_switch(this)" value="{{ $product }}" name="product" autocomplete="off" type="checkbox" @if($product == 'on') checked="" @endif>

<span class="slider round"></span>

</label>


<label class="switch">

<input class="id-target" onchange="click_switch(this)" value="{{ $course }}" name="course" autocomplete="off" type="checkbox" @if($course == 'on') checked="" @endif>

<span class="slider round"></span>

</label>


<button class="btn" type="submit">save</button>


</form>


<script>

function click_switch(el){

    if(el.checked){

        $(".id-target").val("on");

    }else{

        $(".id-target").val("");

    }

}

</script>


switch 1 : on/off  ---> off

switch 2 : on/off  ---> on

switch 3 : on/off  ---> off


When I click the first switch and then I click the save option. The second switch turns on.


switch 1 : on/off  ---> on

switch 2 : on/off  ---> on

switch 3 : on/off  ---> on


The problem is that they save together.


Solution:

The problem is your query is selecting all your buttons, as you’re using a class selector. The fixed JS would look like this.


function click_switch(el){

  el.value = el.checked ? ‘On’ : ‘’

}


However, you don’t need JS as on is the default value for a checkbox. When checked the value will be on and when it is unchecked the value will be an empty. When you set the value attribute on a checkbox it just replace on as the checked value. An unchecked input always returns an empty string.


Answered by:> >David Bradshaw

Credit:> Stack Overflow


Suggested blogs:

>How can I iterate over enum Flag alias?

>How to train a deep learning in case validation with Pytorch result NaN or high loss?s

>Fixing QVariant output issue in Python

>Removing ASCII formatted characters from my CAN messages in Python

>Why `import` does not work in this case (in `exec`)?

>Fix my form calling the wrong Django view error

>How to load static files using Nginx, Docker, and Django?

>How to tell Django "if anything in URL patterns does not match the GET string?

>How can I browser to render links in streamed content in Django?

>Access the "model" attribute for a ListView in Django for template

>How to use a function to print S3 data to HTML using Django?


Submit
0 Answers

What Our Clients Say About Us

Client satisfaction is our ultimate goal. Here are some kind words of our precious clients they have used to express their satisfaction with our service.

Why Choose Us ?

We endow businesses with flexible engagement models based on their unique needs. Our strength lies in state-of-the-art technology and affordable consulting services. Try us for fast POCs, full-fledged applications, or technology consulting. Always available for your service.