I am trying to set the value of a input button to a string variable.i.e"A Guide to the School Bus"; But when the HTML loads up only the first word es up in the button. My code is given below. Thanks for the help.
var title="A Guide to the School Bus";
var htmlString= "<div class="+title+ ">"+"<input type="+"button "+"value="+title+" onclick=loadBook()>"+"</div>";
$(htmlString).appendTo(attachPoint);
And the attachpoint is a reference in the HTML that i got using the following.
var attachpoint=document.querySelector('.buttonAttachPoint');
I am trying to set the value of a input button to a string variable.i.e"A Guide to the School Bus"; But when the HTML loads up only the first word es up in the button. My code is given below. Thanks for the help.
var title="A Guide to the School Bus";
var htmlString= "<div class="+title+ ">"+"<input type="+"button "+"value="+title+" onclick=loadBook()>"+"</div>";
$(htmlString).appendTo(attachPoint);
And the attachpoint is a reference in the HTML that i got using the following.
var attachpoint=document.querySelector('.buttonAttachPoint');
Share
Improve this question
asked Mar 26, 2013 at 20:32
user2208533user2208533
372 silver badges6 bronze badges
2
- The Button just show "A". – user2208533 Commented Mar 26, 2013 at 20:32
-
You could attach your handler rather than put an "onclick" inline:
$(htmlString).appendTo(attachPoint).click(loadBook)
– Stephen P Commented Mar 26, 2013 at 20:55
1 Answer
Reset to default 7The problem is because you're not putting quotes around the attribute values. Try this:
var htmlString= '<div class="'+title+'"><input type="button" value="'+title+'" onclick="loadBook()"></div>';
You can either escape all the "
in your string or, like I have done, just switch between '
and "
. "
will show up as a normal character and '
is used to mark the start and finish of strings.
As a side point you probably wouldn't want to put the variable title
as the class
on the div
as it would add each separate word as a class, so in your example the div would have 6 classes added to it.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744347243a4569758.html
评论列表(0条)