0

I wrote a script to check checkbox when I am clicking on it's respective image. Image have id like checkbox-img-1 and checkbox input have id like checkbox-1 for 1st pair of checkbox and image. For 2nd pair id's are checkbox-img-2 and checkbox-2 and so on. So, whenever I click on image I want to check the respective checkbox. For few images the UI is getting updated but for few images it's not getting updated. What is the possible problem? I searched a bit but all questions were doing mistake of having attr in place of prop. My script is in pure javascript. I tried with jQuery but I am getting same bug. I figured out that content which is not present or not yet displayed in front are not getting selected. The javascript code is:

/* Check option on image click */
        $(".option-check-img").click(function () {
            var checkbox_img_id = $(this).attr("id");
            var checkbox_id     = checkbox_img_id.replace("checkbox-img", "checkbox");
            if(document.getElementById(checkbox_id).checked)
            {
                document.getElementById(checkbox_id).checked = false;
                var d = document.getElementById(checkbox_img_id);
                d.className = "img-circle pointer";
                /*$("#checkbox_id").prop("checked", false);
                $("#checkbox_img_id").removeClass("img-border");
                console.log(document.getElementById(checkbox_id));*/
            }
            else
            {
                document.getElementById(checkbox_id).checked = true;
                var d = document.getElementById(checkbox_img_id);
                d.className += " img-border";
                /*$("#checkbox_id").prop("checked", true);
                $("#checkbox_img_id").addClass("img-border");
                console.log(document.getElementById(checkbox_id));*/
            }
        });

Any solution? Thank you.

4
  • Can you upload code to fiddle? Commented Apr 4, 2016 at 11:29
  • Hi, The code in html+php(magento). Checkboxes are retrieved from db. That will not work on fiddle. Commented Apr 4, 2016 at 11:31
  • @SwapnilBhikule, can you take the generated html from the browser and create a fiddle with that. Commented Apr 4, 2016 at 11:42
  • @Anbarasan I put it on my development site troopr.in. In home page below we have form hashtag/magic search. In that magic search is not updating UI. Commented Apr 4, 2016 at 11:52

6 Answers 6

0

Try to use prop() instead of attr()

JQuery Script not checking check-boxes correctly

Read docs for more info jQuery prop().

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

2 Comments

Hi, in my commented code I am using prop. But it is not working too! I was searching for an answer, all were doing same mistake of using attr but in my case that's not the problem.
The OP has already mentioned that he has used .prop instead of .attr with jquery you can see the commented out code.
0

Are you sure you want to use js for this? You can do this just with css.

*, *:before,  *:after {
  font-family: sans-serif;
  box-sizing: border-box;
}

label {
  display: block;
  margin-bottom: 20px;
  cursor: pointer;
}

span:before {
  content: '';
  
  display: inline-block; 
  width: 15px;
  height: 15px;
  margin-right: 10px;
  
  background: white; /* you can place your image url here */
  border: 4px solid white;
  border-radius: 3px;
  box-shadow: 0 0 0 1px black; 
}

input {
  display: none;
}

input:checked + span:before {
  background: black;
}
<label>
  <input type="checkbox">
  <span>Checkbox 1</span>
</label>
<label>
  <input type="checkbox">
  <span>Checkbox 2</span>
</label>

Comments

0

There is a pure-html solution (Fiddle):

<ul><li>
  <input type="checkbox" id="checkbox-1" />
  <label for="checkbox-1"><img /></label>
</li>
<li>
  <input type="checkbox" id="checkbox-2" />
  <label for="checkbox-2"><img /></label>
</li></ul>

if you need to add styling to your images when checkbox is checked you can use css3 (Fiddle):

input:checked + label img{
  border: 1px solid black;
}

input:not(:checked) + label img{
  border: 1px solid green;
}

// If you want to hide the checkbox
input[type=checkbox] {
  display: none;
}

You should build your html such the checkbox and the image are next to each other.

This approach is much more elegant and doesn't use unnecessary javascript.

1 Comment

Hi, thank you for the answer. But I am using js because my checkboxes are in slider and I am using lot of css for structure. With CSS it might get more complex.
0
            $(".option-check-img").click(function() {
            var $this = $(this);
            var checkbox_img_id = $(this).attr("id");
            var checkbox_id = checkbox_img_id.replace("checkbox-img", "checkbox");
            if ($("#" + checkbox_id + ":checked").length) {
                $("#" + checkbox_id).prop('checked', false);
                $("#" + checkbox_img_id).removeClass("img-border");
                $("#" + checkbox_img_id).addClass("img-circle pointer");
            } else {
                $("#" + checkbox_id).prop('checked', true);
                $("#" + checkbox_img_id).removeClass("img-circle pointer");
                $("#" + checkbox_img_id).addClass("img-border");
            }
        });

Converted your code to proper jQuery Code.

Assumption: ID is of your check-box element.

Make sure no elements have same ID in your DOM.

Updated code to remove alternate-classes too.

Thanks

7 Comments

Hi, thanks for the answer. But it's not working too. Few checkboxes are getting selected, and few fixed checkboxes are not getting clicked.
what is mean by 'fixed check-boxes', here ?? Better you create jsFiddle of your scenario.
If I have checboxes with ids checkbox-1, checkbox-2, checkbox-3 and checkbox-4. Then previously before posting question checkbox-3 was not updating the UI. And now with your answer checkbox-3 is not updating UI.
Problem can be with your CSS, if you have written css rules in combination with IDs. May be you missed out to updated your css for IDs like 'checkbox-3' and rest others.
I am using IDs just for javascript or jquery and all css properties are defined by classes.
|
0

your checkbox id's are not unique.

A different checkbox with the same id is getting checked / unchecked instead of the one you want to manupulate. getElementById / $('#id') will return only one element which ever it finds first.

for instance, in the link that you shared the option Party has a id of checkbox-24 but with the same id there are total of three checkboxes.

similarly for Music with a id of checkbox-27 there are again 3 instances of checkbox with that id.

enter image description here

Your script is correct, but the ids are not unique. Make the id's unique or use someother identifier or combination of identifiers to uniquely identify the checkbox which you want to manipulate.

1 Comment

Hi, yeah, I got the same thing with developer tool, but when I open page source by right clicking and then selecting view page source then I am getting only single checkbox-24.
0

the problem was with infinite loop. Without infinite loop its working. But still I want that infinite slider so I will try something different.

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.