SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript linked checkboxes

    Hello,

    I have an account permission grant application written in PHP and when I tick the 'write' permission checkbox, I also want the 'read' one to be ticked as well.

    I have written the following javascript code but since I'm not that javascript proficient, obviously there's something blocking it from working.

    Code JavaScript:
    function check_read(r,w) {
        if (document.my_perms.w.checked == true){
            document.my_perms.r.checked = true ;
        } else {
            document.my_perms.r.checked = false ;
        }
    }

    and the HTML:

    Code HTML4Strict:
    <form name='my_perms'>
    <input type='checkbox' name='r_8' value='r' />
    <input type='checkbox' name='w_8' value='w' onclick="check_read('r_8','w_8');" />
    </form>

    Thanks a lot!

    Regards,
    Rem.

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try adding an id and using the getElementById method to select the input elements.
    ie
    Code:
    document.getElementById('idname');
    Mashups, DOM Scripting and all the things we call Web2.0 blogger
    http://www.heavyweightgeek.org/
    http://twitter.com/heavyweightgeek

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code JavaScript:
    function check_read(r,w) {
        if (document.my_perms.w_8.checked == true){
            document.my_perms.r_8.checked = true ;
        } else {
            document.my_perms.r_8.checked = false ;
        }
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand what happened, I already replied yesterday. Thank you both for your time but heavyweightgeek's solution did the trick. I also posted the new code but seems it didn't went into the database for some reason.

    Thank you heavyweightgeek for your solution, that worked!

    the new code:

    Code JavaScript:
    function check_w(r,w) {
        if (document.getElementById(w).checked == true) {
            document.getElementById(r).checked = true;
        } else {
            document.getElementById(r).checked = false;
        }
    }

  5. #5
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool.. I know his code would work but I thought I just post another solution that don't require you to put id attribute in your HTML form.

  6. #6
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EDIT: Oops.. sorry about the double post.. I thought the fast reply didn't work.

  7. #7
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you could make it all in one line:

    Code JavaScript:
    function check_w(r,w) {
            document.getElementById(r).checked = document.getElementById(w).checked;
    }
    (function(){arguments.callee()})()


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
  •