Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

get value of checkbox

Posted on 2011-10-13
5
Medium Priority
?
179 Views
Last Modified: 2012-05-12
I am trying to obtain the value of the checkbox (whether it is checked or not) using this code:

  $('input[type="checkbox"]').each(function ()
            {
                var str = $(this).attr("ID");
                var last = str.split("_").pop();
                customstring[last] = $(this).val();
            });


$(this).val();

is returning "on"  whether the box is checked or not!  Obviously that won't do.  :)


How do I do it the correct way?
0
Comment
Question by:Tom Knowlton
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 36965640
http://jsfiddle.net/tD5Ln/

versus

http://jsfiddle.net/k6zCs/

<input type="checkbox" id="c_chk0" value="0" />
<input type="checkbox" id="c_chk1" value="1" />
<input type="checkbox" id="c_chk2" value="2" />
<input type="checkbox" id="c_chk3" value="3" />
<input type="checkbox" id="c_chk4" value="4" />

Open in new window

var customstring = {};
$('input[type="checkbox"]').each(function (){
    var str = $(this).attr("ID");
    var last = str.split("_").pop();
    customstring[last] = $(this).val();
});
alert(JSON.stringify(customstring));

Open in new window


VERSUS

<input type="checkbox" id="c_chk0" />
<input type="checkbox" id="c_chk1" checked="checked" />
<input type="checkbox" id="c_chk2" />
<input type="checkbox" id="c_chk3" />
<input type="checkbox" id="c_chk4" />

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 36965646
check this one : http://jsfiddle.net/k6zCs/2/

var customstring = {};
$('input[type="checkbox"]').each(function (){
    var str = $(this).attr("ID");
    var last = str.split("_").pop();
    customstring[last] = $(this).is(":checked")?"on":"off";
});
alert(JSON.stringify(customstring));
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36965651
???
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36965662
lol

to get the value (checked or not checked use :  $(this).is(":checked"); // return true or false

else you're trying to get the value, look like your checkboxes don't have value, the default one is "on"
0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 36965702
thx!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question