1

I have a simple javascript issue. I have a checkbox and a input box.

when the user inputs text into an input box (onkeydown) I want it to check the corresponding checkbox in the row.

My checkbox's all share the same name for an array checkbox[].

my simplified syntax is:

<table>
<?php if(isset($records)) : foreach ($records as $row) : ?>
    <tr>
        <td>
            <input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
        </td>
        <td>
            <input type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" onclick="document.getElementById('editcustomer<?php echo $row->id ?>').checked = true;">
        </td>
    <tr>
<?php endforeach ; ?>
</table>

As the name of the checkbox is not unique, only the value thereof how do I tell the javascript which checkbox to check?

<input type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" onclick="document.getElementById('editcustomer<?php echo $row->id ?>').checked = true;">

Help appreciated as always.

Thanks

2
  • call a method on onkeydown / onchange with passing this as a parameter, then in that function use function abc(elem) { $(this).parent().next().find(':checkbox').attr('checked',false); } Commented Mar 4, 2013 at 10:49
  • can you assist by looking at the fiddle? jsfiddle.net/A4GaF/1 Commented Mar 4, 2013 at 11:14

1 Answer 1

1

HTML

<input type="checkbox" name="checkbox[]"/>
<input type="text" name="name1" id="name1" />
<br>
 <input type="checkbox" name="checkbox[]"/>
<input type="text" name="name2" id="name2" />
 <br>
 <input type="checkbox" name="checkbox[]" />
<input type="text" name="name3" id="name3" />

Jquery

$(':text').change(function(){
    $(this).prev(':checkbox').attr('checked','true');
});

Hope this helps. Regards.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi X-Factor. thanks for your time. not quite working as per fiddle, jsfiddle.net/A4GaF/1. any ideas why? how does the script know which checkbox it must find. so on the fiddle, if I type in any box it should check the corresponding checkbox. Thanks
I updated your code, but you have to add jquery for that @Smudger

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.