2
<div id="myDiv1">
<table>
<tr id="row">
</table>
</div>
<div id="myDiv2">
<table>
<tr id="row">
</table>
</div> 

Here I need to append different '<td>'s into these two . I have tried this code $('#row').append() but its only putting '<td>'s into the row of myDiv1. Can anyone help me to do this.

3
  • 3
    ID attribute should be unique in DOM. Commented Jan 20, 2015 at 10:59
  • 1
    use class instead of id Commented Jan 20, 2015 at 10:59
  • Browsers store a fast ID-lookup as a single dictionary entry (of ID vs a single element). This means jQuery and Javascript can only find the first match by ID. Use classes instead. Commented Jan 20, 2015 at 11:04

2 Answers 2

2

There are multiple erros in your markup:

  1. No closing <tr>-tags
  2. Non-Unique ID's

So the ID-Attribute has to be unique in its scope. See here for further information.

Your mark-up should be looking like this:

<div id="myDiv1">
    <table>
        <tr class="row">
        </tr>
    </table>
</div>
<div id="myDiv2">
    <table>
        <tr class="row">
        </tr>
    </table>
</div> 

With the right markup your jQuery just works fine:

$('.row').append('<td>test</td>');

Demo

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

Comments

0

You should change the ID attribute to a class.

But why are you using two tables? maybe you need to clean the html and make it as bellow.

<div id="myDiv1">
    <table>
        <tr class="row">
        </tr>
        <tr class="row">
        </tr>
    </table>
</div> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.