2

I am trying to fire parent click event whenever an input change happens. Below is my code, it is firing Click event for two times.

                  $('TABLE TBODY TR TD INPUT').change(function()
                  {
                   $(this).parents('TD').click();
                  });
                  $('TABLE TBODY TR TD').click(function()
                  {
                   alert("Clicked me...");
                  });

Here is my HTML Code

      <table>
      <tr><td>foo<input type="radio"></td><td>foo<input type="checkbox"></td></tr>
      <tr><td>foo<input type="radio"></td><td>foo<input type="checkbox"></td></tr>
      <tr><td>foo<input type="radio"></td><td>foo<input type="checkbox"></td></tr>
      </table>

Where is the mistake...

Reddy
  • 1,327
  • 3
  • 23
  • 44

2 Answers2

1

try something like. this

$('TABLE TBODY TR TD INPUT').change(function() {
   alert("Clicked me...");
});
rrapuya
  • 298
  • 2
  • 10
  • um.. ? why does your jsfiddle link to a jsfiddle I made for a different question last night? http://stackoverflow.com/questions/6514985/jquery-slideup-problem/6515002#6515002 – matchew Jun 29 '11 at 18:29
1
$('TABLE TBODY TR TD INPUT').change(function() {
    clicky(this.parents('TD'));
});
$('TABLE TBODY TR TD').click(function clicky(parentElement) {
    alert("Clicked me...");
});

so the input is also in td so both are being clicked. this only returns one alert, but I have a feeling you dont want to alert, but rather change the parent element.

but I bet you probably want the following

$('TABLE TBODY TR TD INPUT').change(function() {
    clicky($(this).parents('TD'));
});

 function clicky(parentElement) {
    $(parentElement).toggleClass('clicked');

};

live example: http://jsfiddle.net/nFtzJ/

here every time the input changes, the parent <td> has the class .clicked toggled, of course this can be changed, but its almost a faux pas to use alert(); in production.

matchew
  • 19,195
  • 5
  • 44
  • 48
  • 1
    for the record, this question was accepted before the **but I bet you probably want the following** addition. – matchew Jun 29 '11 at 03:01