0

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;
}

3 Answers 3

2

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

var form_object = document.getElementsByName(checkbox_name+"["+i+"]")[0];
Sign up to request clarification or add additional context in comments.

1 Comment

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
0

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;
}

Comments

0

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;
}

2 Comments

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; }
Did you assign a reference to the form to the variable form first as specified?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.