Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What is want is - when the checkbox is checked option no. 5 in select list will be selected and when the checkbox is unchecked option no. 0 will be selected in the select list.

The select list and the checkboxes are generated dynamically in the php code as below :

echo "<select name='coupons".$i."' id='coupons".$i."'>";
------- All Options --------
echo "</select>";


<input type='checkbox' name='myCheckbox[]' value='<?php echo $i."_".$row->id; ?>' onclick='setCCode("myCheckbox[]",<?php echo $i;?>)'> 

 -----------------------------------------------------------------------------

Solved the second requirement on my own now ..... thanks to all for your inputs

just added the following line in the checkAll() within the for loop

setCCode(children[i],i+1);

The javascript function :

function setCCode(checkbox_name,i) 
{   
    var form_object = document.getElementsByName(checkbox_name+"["+i+"]");
    var selname = document.getElementsByName("coupons"+i)[0];

    if(form_object.checked) {
            selname.selectedIndex = 5;
    }
    else {
        selname.selectedIndex = 0;
    } 
}

The above issue is solved....... thanks to all

Now what i need to do is when a user checks a checkbox to select or deselect all the dynamically generated checkboxes on the form the above logic should work.

<input type='checkbox' name='checkall' onChange="checkAll(this, 'myCheckbox[]')">
<span class="chkall">Check / Uncheck All</span>

<input type='checkbox' name='myCheckbox[]' value='<?php echo $i."_".$row->id; ?>' onclick='setCCode(this,<?php echo $i;?>)'>

The javascript code i am using to select/deselect all checkboxes on form is as below :

function checkAll(parent, field)
{
    var children = document.getElementsByName(field);
    var newValue = parent.checked;

    for (i = 0; i < children.length; i++){
    if (children[i].disabled == false) {    
        children[i].checked = newValue;
    }
    }
}

function setCCode(Sender,i) 
{   
  document.getElementsByName("coupons"+i)[0].selectedIndex = Sender.checked ? 5 : 0;
}
share|improve this question
add comment (requires an account with 50 reputation)

3 Answers

up vote 0 down vote accepted

You can pass refference to the checkbox itself as a parameter

<input type='checkbox' name='myCheckbox[]' value='<?php echo $i."_".$row->id; ?>' onclick='setCCode(this,<?php echo $i;?>)'>

function setCCode(Sender,i) 
{   
  document.getElementsByName("coupons"+i)[0].selectedIndex = Sender.checked ? 5 : 0;
}
share|improve this answer
thanks jan. that worked ..... – Sandy505 Sep 23 '11 at 6:41
add comment (requires an account with 50 reputation)

getElementsByName returns an array of objects. Replace the line with:

var form_object = document.getElementsByName(checkbox_name+"["+i+"]")[0];
share|improve this answer
getting the error message as below when i am doing what u suggested Error: form_object is undefined Source File: localhost:82/rajib/admin/free_members_ajax.php Line: 71 – Sandy505 Sep 23 '11 at 6:29
add comment (requires an account with 50 reputation)

If you have a reference to the form that the checkbox is in, and it has a unique name in the form, then you can access it as:

form_object = form.elements[ checkbox_name + "[" + i + "]" ];

and you can also use the ternary operator to make the code more concise:

selname.selectedIndex = form_object.checked? 5 : 0;

Edit

Sorry, missed the obvious. If you pass a refereference to the checkbox in the handler, then you can also get the form (all form controls have a form property that references the form they are in). So as Jan Pfeifer suggested (abbreviated markup):

<input ... onclick='setCCode(this, <?php echo $i;?>)'>

then the script is just:

function setCCode(checkbox, i) 
{   
    checkbox.form.elements['coupons' + i].selectedIndex = checkbox.checked? 5 : 0;
}
share|improve this answer
Error: form_object is undefined Source File: localhost:82/rajib/admin/free_members_ajax.php Line: 72 function setCCode(checkbox_name,i) { var form_object = document.forms[0].elements[ checkbox_name + "[" + i + "]" ]; var selname = document.getElementsByName("coupons"+i)[0]; selname.selectedIndex = form_object.checked? 5 : 0; } – Sandy505 Sep 23 '11 at 6:36
Did you assign a reference to the form to the variable form first as specified? – RobG Sep 23 '11 at 9:22
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.