SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Body
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    One function to rule them all...

    Trying to write a function that will enable/disable a drop down box of a corresponding checkbox. Rather than a function for each checkbox, I'd like to be able to just use 1 function to do it all.

    I can't figure out how to pass the name of the corresponding dropdown to javascript though, so that I can disable/enable it after a corresponding checkbox is selected/deselected.. Here's my code:

    HTML
    Code:
    <input type='checkbox' name='veggies' value='veggies' onchange="disableFormElement([This is what I need.. how to send the name 'veggies', or 'fruits', or whatever to the function dynamically)" /><select name="veggieType" disabled>
    		<option>Choose One</option>
    		<option value="corn">Corn</option>
    		<option value="bean">Bean</option>
    		</select>
    Javascript
    Code:
    function disableFormElement(disableIt){
    //if the drop down is disabled, the user has selected the corresponding checkbox, so enable it.
    	if (document.pre.[This is what I need].disabled==true){
    	document.pre.[This is what I need].disabled=false;
    	} else //they have deselected the checkbox, to disable the dropdown.
    	{
    	document.pre.[This is what I need].disabled=true;
    	}
    Thanks!

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure how you have structured your checkbox and select box with different names, but if you are using (php), then the following example will help you out:
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    </head>
    <script language="javascript" type="text/javascript">
    function doCheck(chk, sel){
    	if(chk.checked == true){
    		sel.disabled = true;
    	}
    	else{
    		sel.disabled = false;
    	}
    }
    </script>
    <body>
    <form name="frm1" id="frm1" method="post" action="">
    <table cellspacing="1" cellpadding="2" width="500">
    <?php
    for($i = 1; $i <= 10; $i++){
    ?>
    <tr>
    <td width="10&#37;">
    <input type="checkbox" onClick="doCheck(this, this.form.sel_<?php echo $i;?>)" name="chk[]" id="chk_<?php echo $i;?>" value="<?php echo $i;?>" />
    </td>
    <td width="90%">
    <select name="sel_<?php echo $i;?>" id="sel_<?php echo $i;?>">
    	<option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>
    </td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="2">
    <input type="button" name="btn1" value="Add" onClick="doAddNew();">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •