SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing form elements to a function.

    Hi all,

    Im so totally not down with javascript. I am trying to make a universal function where i can pass the form name, checkbox name and text field name. Then enabled or disable the text field based on the check box status.

    Sounds good yeah? It works if i have the form element names in the function, but i want to pass them to the function instead.

    Thanks for looking...

    Cheers,
    p.

    Code:
    function myfunction(a, b, c)
    {
    	alert(a + b + c);
    	if (document.a.b.checked == true) 
    	  { document.a.c.disabled = true; }
    	else
    	  { document.a.c.disabled = false; }
    }
    
    <form name="form1"> 
    <input type="checkbox" name="check1" onClick="myfunction('form1', 'check1', 'text1')"/>  
    <input type="text" name="text1" value="Type some text"> 
    </form>

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function myfunction(chkId, txtId)
    {
        var chk = document.getElementById(chkId);
        var txt = document.getElementById(txtId);
    
        txt.disabled = chk.checked;
    }
    HTML Code:
    <form action="...">
      <div>
        <input id="check1" name="check1" type="checkbox" onclick="myfunction('check1', 'text1')"/>
        <input id="text1" name="text1" type="text" value="Type some text"/>
      </div>
    </form>
    (Your use of the XML-style self-closing tag syntax leads me to believe you're using XHTML, and event attribute names must be all-lowercase in XHTML; thus 'onclick' rather than 'onClick'.)
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Tommy,

    Great thanks for the info, though i am having trouble getting the checkbox to function properly. i would like the text box to be disabled to begin with, then enabled when the box is checked.

    Code:
    <input name='LinkCatChk' type='checkbox' onclick=\"change('LinkCatChk', 'LinkCatTxt')\"'>\n";
    					echo "			<input name='LinkCatTxt' type='text' disabled='true' size='40' maxlength='255' value='New Category'>\n";
    I have tried changing the txt.disabled = chk.checked line in the function to unchecked and enabled, but it isnt helping, will i need an if statement or such?

    Thanks for the help.
    p.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, there is no unchecked. Just use the not operator.

    txt.disabled = !chk.checked;

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's right. The properties are called 'disabled' and 'checked' (since they correspond to the HTML attributes with the same names). They are implemented as booleans, so just invert them with the logical not operator ('!').
    Birnam wood is come to Dunsinane

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah yes,

    the olde ! operator, i use it all the time in php. i'm such a javascript no0b.

    Thanks for your help guys your rad

    Cheers,
    paul.


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
  •