1

I have checkbox html.

<label><input type="checkbox"><span>Air Conditioning</span></label>
<label><input type="checkbox"><span>Cable TV Service</span></label>
<label><input type="checkbox"><span>Private Bathroom</span></label>

I need to have seperate value for each checkbox, so I can get those values from all and store in variable. For example

<label><input type="checkbox" data="1"><span>Air Conditioning</span></label>
<label><input type="checkbox" data="2"><span>Cable TV Service</span></label>
<label><input type="checkbox" data="3"><span>Private Bathroom</span></label>

So if all the 3 are selected I will need my variable as

room_features = '1-2-3';

If any one is then unchecked, variable will be updated as

room_features = '2-3';

So on every change on checkbox, variable should be updated. I am confused if either adding data="1" etc is fine for checkboxes? I normally do for anchors when there is single double quotes.

2

6 Answers 6

2

Live Demo

You should use value in each checkbox. for ex:

HTML:

<input type="checkbox" value="1" checked />
<input type="checkbox" value="2" checked />
<input type="checkbox" value="3" />

JQuery:

var searchIDs = $("input:checkbox:checked").map(function(){
        return this.value;
    }).toArray();
Sign up to request clarification or add additional context in comments.

Comments

1

html

<label><input type="checkbox" checked="checked" data="1"><span>Air Conditioning</span></label>
<label><input type="checkbox" checked="checked" data="2"><span>Cable TV Service</span></label>
<label><input type="checkbox" data="3"><span>Private Bathroom</span></label>

JS

 var selectedvalue=[];
    $(":checkbox:checked").each(function(){

    selectedvalue.push($(this).attr("data"));

    });

alert(selectedvalue.join("-"));// your desired result

demo

reference Arrays and checked-selector

Comments

1

You can use .map() method, note that I have added value attribute to the element instead of data as an input should have a value otherwise what's the point of using it? If you want to use data-* attributes, you should specify an identifier. Something like <input type="checkbox" data-id="1">, then you can get the value using jQuery data() method: $(elem).data('id');.

<label><input type="checkbox" value="1"><span>Air Conditioning</span></label>
...

var vals = $('input[type=checkbox]:checked').map(function(){
     return this.value; // return $(this).data('whatever');
}).get().join('-');

get() returns an array, if you need an array you can remove the .join() method which returns a string.

Comments

1

try this dynamic one

var room_features = "";

$('[type=checkbox]').change(function () {
var data = "-" + $(this).attr('data');
if (room_features.indexOf(data) != -1) {
    room_features = room_features.replace(data, "");
    alert(room_features);
} else {
    room_features = room_features + data;
    alert(room_features);
}
});

Demo Fiddle

Comments

1

javascript

getElementById("chkbox1").checked=true;

jquery

$("#chkbox1").prop("checked");

or

$("#chkbox1").attr("checked");

Comments

0

check this Jsfiddle

$("#btn").click(function(event){
    var selectedvalue=[];
$(":checkbox:checked").each(function(){

selectedvalue.push($(this).attr("data"));

});

alert(selectedvalue.join("-"));// your desired result

  })

2 Comments

What is the difference between this and @Riturajratan's answer? You just add a click handler, even variables are the same.
with this button you can check any time

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.