user2255654
user2255654

Reputation:

How do I give an id to an appended element?

The one I'm talking about is one where I have the code appear literally, a string. How do I combine it with actual code, like <div id="thing">string goes here</div>?
I've tried making the div and id part of the content inside the (' ') but it doesn't work. When I do that, the content never gets appended.

Here's the code:

$('#placeToAppendTo').append
('&lt;br&gt;&lt;div&gt;&lt;input&nbsp;type="text"&nbsp;class="textfield"&nbsp;id="text'+textClicked+'"&lt;/div&gt;');

Upvotes: 0

Views: 99

Answers (2)

adeneo
adeneo

Reputation: 318352

or in jQuery:

$('<div />', {id:'thing', text:'string goes here'});

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324820

To get the HTML you presented, you would do something like this:

var elem = document.createElement('div');
elem.id = "thing";
elem.appendChild(document.createTextNode("string goes here"));

I hope this helps, since your question isn't particularly clear.

Upvotes: 4

Related Questions