javascript - Setting the Input Value attribute to a variable with space. - Stack Overflow

I am trying to set the value of a input button to a string variable.i.e"A Guide to the School Bus&

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
Add a ment  | 

1 Answer 1

Reset to default 7

The 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信