JDS
JDS

Reputation: 16978

jquery appending button error!

Just a quick little thing I'm obviously doing wrong:

theDiv.append('<input type="button" id='+id+ 'value='+value+'></input>');

Where id and value are variables that I assign to as strings. Why wouldn't this work? What's the quick work-around?

thanks.

Upvotes: 1

Views: 163

Answers (3)

Niklas
Niklas

Reputation: 30002

It does work. You are just missing your quotation marks around the attributes:

theDiv.append('<input type="button" id="'+id+ '" value="'+value+'" />');

example:

http://jsfiddle.net/niklasvh/LqvyQ/

Upvotes: 2

MoarCodePlz
MoarCodePlz

Reputation: 5176

If I have id = 1 and value = 15 then this is what makes it to the page

<input type="button" id=1value=15></input>

When you should really have

<input type="button" id="1" value="15"></input>

Upvotes: 1

user113716
user113716

Reputation: 322492

You need a space between the id and the value attribute:

 // --------------------------------------v
.append('<input type="button" id="'+id+ '" value="'+value+'">');  

I also added double quotes around the attribute values, and removed the closing </input> tag.

This results in:

<input type="button" id="id" value="value">

...given "id" as the value of id, and "value" as the value of value.

The way you had it, the value of the id attribute, and the value attribute itself were merged, so if the value of id was "id", it would look like:

<input type="button" id=idvalue=value>

Upvotes: 2

Related Questions