admin管理员组文章数量:1245710
Im trying to add a div element that contains a label but it does not work
Can you please let me know how to fix it? My fiddle
html
<div id="first-tab">
js
$(document).ready(function(){
$( "#first-tab" ).append("<div><label for="name">Test</label></div>")
})
Im trying to add a div element that contains a label but it does not work
Can you please let me know how to fix it? My fiddle
html
<div id="first-tab">
js
$(document).ready(function(){
$( "#first-tab" ).append("<div><label for="name">Test</label></div>")
})
Share
Improve this question
edited May 29, 2014 at 2:56
Alexander Kireyev
10.8k13 gold badges67 silver badges105 bronze badges
asked May 29, 2014 at 2:47
user1050619user1050619
20.9k89 gold badges251 silver badges430 bronze badges
6 Answers
Reset to default 7Working demo :)
http://jsfiddle/d5Lqs/
- if you keen: jquery .html() vs .append()
Issue use '
doubles quotes were not used correctly.
code
$(document).ready(function(){
$( "#first-tab" ).append('<div><label for="name">Test</label></div>');
});
In case of jQuery when you use any HTML tag inside a method you enclose it in double quotes like this
"<div>Anything.....</div>"
Inside these double quotes if you again use double quotes like this
"<div id="divSection" class="newClass">Anything.....</div>"
then editor wont recognize the double quotes of id divSection and class newClass.
There are number of ways you can solve this, One of the way like using single quote inside double quote as below
"<div id='divSection' class='newclass'>Anything.....</div>"
But using this will give SPCAF error if you follow coding standard given by Microsoft as "UseConsistentOfQuotationMarks" So this is the be best way
$(document).ready(function(){$("#section").append("<div><label for=\"name\">Test</label></div>");});
Here whenever there is a double quotes inside double quotes prefix the inner double quote with a "forward slash" like above
Fix your quotes:
$("#first-tab").append("<div><label for='name'>Test</label></div>")
You have a couple of issues - you need to close the <div></div>
and use single quotes, or escape the double quotes, in your JavaScript.
$(document).ready(function(){
$( "#first-tab" ).append("<div><label for='name'>Test</label></div>")
});
See it working in the updated jsFiddle.
You already doing it right. Just change a little bit like:
$(document).ready(function(){
$( "#first-tab" ).append("<div><label for='name'>Test</label></div>")
});
Here I have change "name" to 'name', which renders the wrong html.
Wouldn't it be the same thing to use something like this? It changes the insides of the element with the ID of first-tab
.
document.getElementById('first-tab').innerHTML = "<div><label for='name' >TEST</label></div>";
Here is the fiddle
本文标签: javascriptAdding a label with div using JqueryStack Overflow
版权声明:本文标题:javascript - Adding a label with div using Jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740245011a2248077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论