checkbox values in javascript?

Posted on 2006-06-06
Last Modified: 2009-12-16

How can I get checkbox values in Javascript?

function passit()

myhazard = document.all.hazard.checked;<--------??????
theResult = myhazard

window.returnValue = theResult
Question by:suran78
    LVL 30

    Expert Comment


    myhazard = document.formname.hazard.value;

    if you want to get the value for checked checkbox,

    myhazard = document.formname.hazard.checked?document.formname.hazard.value:'';
    LVL 30

    Accepted Solution

    here's an example,

    <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
    <title>Author: Third Santor</title>
      function getCheckboxValues(obj){
        for(var i=0; i<obj.length; i++){
            if((obj.elements[i].type == 'checkbox') && (obj.elements[i].checked)){
    <body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
    <form name="form1" method="post" action="" onsubmit="">
    <input type="checkbox" name="chk1" value="value1">
    <input type="checkbox" name="chk1" value="value2">
    <input type="checkbox" name="chk1" value="value3">
    <input type="button" value="Click" onclick="getCheckboxValues(this.form);">

    Author Comment

    Thanks this is working.

    i have another open question at

    Author Comment

    another small question:

    How can I index a row in Javascript?

    hazard[rowid].innertext = values  <-------------??????


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Both jQuery and Mootools offer some nice, easy to use scripts. Mootools is best known for their interactive slideshows and jQuery for their menus, but both have many other uses as well.   jQuery is basically a toolkit with many DOM (http://en.wik…
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
    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…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now