最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin9浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论