2

I'm just learning JQuery, I'm stuck on the following.

I have a HTML table (created by PHP, displaying the result of a MYSQL query).

The first column of the table has a drop down menu which allows the user to change the value of the cell, and update the database. I'm using, .toggle, .post to make that work. My issue is that only the first row works, which I believe is because I'm targeting the ID(s) which are duplicated.

So, can someone point me toward the correct technique. By biggest problem is not knowing the right question to ask....

Do I somehow dynamically create unique ID's for each row? But if so, how do I pass those, or otherwise get JQuery to target each row and it's content as it's selected by the user?

Thanks

--

2
  • Can you share the code you're trying? If by Id you mean HTML IDs, these are supposed to be one per page, so you might want to be using classes instead. Commented Mar 18, 2013 at 14:50
  • Yeah it's not allowed to set an ID more than once inside a HTML document (that's really important). Commented Mar 18, 2013 at 14:51

3 Answers 3

2

It would be better to simply use classes. Replace id=" with class=", then target based on the clicked row rather than id.

var theRow = $(this).closest("tr.myclass");
theRow.doSomething();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that seems to work. It will take me a bit of time to really understand and implement it....but know I know the direction to go. Thanks again.
2

don't look at it from the point of view of ids. Look at it from the POV of a tree. Every node in a DOM tree knows where it is: its parents, its siblings, its children. If something happens on a row, you use the location of whatever that got clicked on to identify "where" you are and get the necessary related data that way.

e.g.

<tr>
   <td>Row 1</td>
   <td><button click="showRowNumber(this);">click</button></td>
</tr>
<tr>
   <td>Row 2</td>
   <td><button click="showRowNumber(this);">click</button></td>
</tr>
<script type="text/javascript">
function showRowNumber(el) {
   alert($(el).prev().text());  // outputs "Row 1" or "Row 2"
}
</script>

No IDs, just some tree manipulation code.

Comments

0

As Kevin said you need to use classes instead of an id.

Inside the class event handler, in this case click, use this such that you are specifically referring to the element that is clicked, like this:

$('.toggler').click(function(){

   $(this).append('clicked element id: '+this.id); //will show the unique id for the toggled element
    //$.post(); will want to put your $.post inside to also make use of "this"
});

To help you learn you can also do this on byTagName, in this case by table cell (td):

$('td').click(function(){

   $(this).append('clicked element id: '+this.id); //will show the unique id for the toggled element
    //$.post(); will want to put your $.post inside to also make use of "this"
});

More uses of this: If you are deleting or adding rows to your table and need to keep track of which row you are working on then you can use jQuery or plain only javascript inside the click event like this: to show what number row you clicked on:

$("table tr").click(function(){
    alert('jQuery: '+$(this).index()); //jQuery
    alert('javascript: '+this.rowIndex); //javascript
});

Lastly if a row don't exist when the page loads you need to use event delegation by using jQuery's on() method. This could also be the reason that you can't click on additional rows besides the first one.

$(document.body).on('click', "table tr", function(){
    alert('jQuery: '+$(this).index()); //jQuery
    alert('javascript: '+this.rowIndex); //javascript
});

Comments

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.