0

I have this markup

     <table id="myexample" class="myclass">
     <thead>
     <tr>
     <th><input type="checkbox" value="tableoption1"> Username</th>
     <th> Something </th>
     <th> Something </th>
     <th> Something </th>
     <th> Something </th>
     <th> Something </th>
      <th> Something </th>

     </tr>
     </thead>
     <tbody>
     <tr>
     <td> <input id="user1" type="checkbox" value="tableoption1">  John Smith </a></td>
     <td> Nickname</td>
     <td> Address </td>
     <td> Registerd</td>
     <td>10-10-2014 </td>
     <td>09-11-2014</td>
     <td>03-02-2014</td>
     </tr>
</table>

How can I select with Jquery the input element with id=user1 ?

I have already tried this but doesnt work

 $("#users tbody tr td input").click(function(){ 
      alert ("success");
 });

Any suggestion ?

3

3 Answers 3

2

This should work:

$('#user1').click(function(){ 
      alert ("success");
 });
Sign up to request clarification or add additional context in comments.

4 Comments

If you got an answer please submit it.
@Flopert thanks this worked fine.Is there a way to select all classes starting with user for example to be able to select at once user1,user2 etc etc ?
If I understood correctly what you want to do, you can just pass another id like this: $('#user1, #user2, #user3')
Yes but if there is a way to declare all classes starting with "user" instead of declaring user1,user2 ?
2
$( "#user1" )

Try this I think thats what you are asking.

Comments

1

If you want to control when checkbox is checked:

$('#user1').click(function(){ 
    if($(this).is(":checked")){
        alert("checked!");
    }
});

2 Comments

thanks @Borachio this also works. Is there a way to select all ids that contain the "user" for example to multiple select user1,user2 user3 etc etc without writing smt like this $('#user1, #user2, #user3') ?
In this case, it's better work with classes, like: <input type="checkbox" class=".userChoice"/> and in jquery: $(".userChoice") without hash character. check: jsfiddle.net/pxeLgy6n

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.