SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change value of checkbox if checked?

    I have a checkbox in a form. The form is submitted via ajax everytime a users makes any chenges to the drop down menu's in it and returns an updated value. That part is fine.
    the part I'm having issues with is the checkbox in the form. Initially it is set to checked. The proble I have is that when the ajax script runs it takes the value from the checkbox regardless of whether it is checked or not!

    How can I get around this?
    Is there a way to set an initial value and another checked valus on the checkbox?
    I've had a look and can't find anyway to do this.

    Please help!

  2. #2
    SitePoint Evangelist speda1's Avatar
    Join Date
    Jan 2002
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose you could modulate the 'value' attribute of the checkbox prior to making the AJAX call. The 'checked' attribute is separate from that.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Check that the checkbox is checked, before assigning its value.

    Code javascript:
    var form = document.getElementById('myForm');
    form.onsubmit = function () {
        var form = this;
        ...
        var checkbox = form.elements.myCheckbox;
        checkboxValue = checkbox.checked ? checkbox.value : '';
        ...
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •