SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist mumford1's Avatar
    Join Date
    Sep 2002
    Location
    UK
    Posts
    573
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do you check/uncheck a checkbox using Javascript

    Can anyone tell me how you check or uncheck a checkbox using javascript?

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Code:
    document.[form name].[checkbox name].checked = true;
    Replace [form name] and [checkbox name] with the names of your form and checkbox, respectively . Set it to false to uncheck the box.

  3. #3
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey vinnie, along these same lines....how would you refer to this element using getobjectbyid[]?
    Aaron Brazell
    Technosailor



  4. #4
    SitePoint Evangelist mumford1's Avatar
    Join Date
    Sep 2002
    Location
    UK
    Posts
    573
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ta very much

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Sketch
    hey vinnie, along these same lines....how would you refer to this element using getobjectbyid[]?
    Not sure what you mean. Are you talking about getElementById? If you want full DOM compliance, then you can have something like this:

    JS
    Code:
    document.getElementById("myCheckbox").setAttribute("checked", "checked");
    HTML
    Code:
    <label for="myCheckbox">Restart server?</label> <input type="checkbox" name="myCheckbox" id="myCheckbox" />
    If you're talking about something else, let me know and I'll try to work up a solution .

  6. #6
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No that's what I meant. It just seems like using getelementbyid always coughs for form fields and I haven't been able to figure it all out yet.
    Aaron Brazell
    Technosailor



  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Sketch
    No that's what I meant. It just seems like using getelementbyid always coughs for form fields and I haven't been able to figure it all out yet.
    Form fields require a "name" attribute per the HTTP spec, and a lot of people forget to use IDs as well, which are required for <label> tags to work properly, as well as for scripts (scripts using getElementById() and getElementsByTagName() at least) to work properly. Another collection like getFormFieldsByName() would have been nice in the DOM to avoid this confusion and replace the old document.forms[].elements[] collections.

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True vinnie, but I still prefer the collections to the literal properties

    this
    document.forms['form1'].elements['input1']

    rather than this
    document.form1.input1

    Sure, it's more typing, but FAR less problematic - and I'm not really that lazy to type a dozen extra characters
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  9. #9
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by beetle
    True vinnie, but I still prefer the collections to the literal properties

    this
    document.forms['form1'].elements['input1']

    rather than this
    document.form1.input1

    Sure, it's more typing, but FAR less problematic - and I'm not really that lazy to type a dozen extra characters
    I agree; I was just giving the "quick-fix" version . The problem with the document.forms[] collection (or the shortened document.[form name] sytnax) as I understand it is that when XHTML is sent to the browser as XML the scripts using those collections will fail though.


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
  •