Tony H
Tony H

Reputation: 347

Select dynamally created element in jquery

Given this HTML table:

<table id="#myTable">
  <tr id="#row123"><td>Content</td></tr>
</table>

Add in a row with jquery:

$('#myTable').prepend('<tr id="#row456"><td>More content</td></tr>');

Later on I want to select the #row456 row that was created. How do I do that? $('#row456') does not work?

Upvotes: 0

Views: 29

Answers (2)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382102

Remove the # from the id when creating the elements in HTML:

$('#myTable').prepend('<tr id="row456"><td>More content</td></tr>');

#someid is the syntax of a jQuery selector selecting the element having someid as id (reference). You also have the same syntax for selection by id in CSS.

Upvotes: 3

user6139265
user6139265

Reputation:

Don't use # before id

$('#myTable').prepend('<tr id="row456"><td>More content</td></tr>');

Also your HTML

<table id="myTable">
  <tr id="row123"><td>Content</td></tr>
</table>

Upvotes: 2

Related Questions