admin管理员组文章数量:1182735
I'm writing a form validation script for a form with multiple ratings, and I'd like to insert a bit of text that says "give a rating!" for each rating the user misses. I wrote the code below to do this, but I'm running into a problem where the give_rating node is only appended to the last node on the form. I know that this is because appendChild basically moves a node instead of duplicating it, and I tried solving this using cloneNode but that just breaks my JS entirely.
Anyway, here's the code. What am I doing wrong?
Thanks for your help,
Chris
var give_rating = document.createElement('span');
give_rating.className='small red';
give_rating.innerHTML = '<strong> Give a rating!</strong>';
document.getElementById('rating1').appendChild(give_rating);
document.getElementById('rating2').appendChild(give_rating);
When I use the code above code give_rating is only appended to 'rating2'.
document.getElementById('rating1').appendChild(give_rating.cloneNode(True));
document.getElementById('rating2').appendChild(give_rating.cloneNode(True));
When I use this code the entire script fails. How do I add an instance of "Give a rating!" for each rating on my form that the user fails to fill out?
I'm writing a form validation script for a form with multiple ratings, and I'd like to insert a bit of text that says "give a rating!" for each rating the user misses. I wrote the code below to do this, but I'm running into a problem where the give_rating node is only appended to the last node on the form. I know that this is because appendChild basically moves a node instead of duplicating it, and I tried solving this using cloneNode but that just breaks my JS entirely.
Anyway, here's the code. What am I doing wrong?
Thanks for your help,
Chris
var give_rating = document.createElement('span');
give_rating.className='small red';
give_rating.innerHTML = '<strong> Give a rating!</strong>';
document.getElementById('rating1').appendChild(give_rating);
document.getElementById('rating2').appendChild(give_rating);
When I use the code above code give_rating is only appended to 'rating2'.
document.getElementById('rating1').appendChild(give_rating.cloneNode(True));
document.getElementById('rating2').appendChild(give_rating.cloneNode(True));
When I use this code the entire script fails. How do I add an instance of "Give a rating!" for each rating on my form that the user fails to fill out?
Share Improve this question asked Nov 19, 2010 at 4:52 ChrisChris 1,3335 gold badges19 silver badges33 bronze badges2 Answers
Reset to default 16JavaScript is case-sensitive: True
should be true
(lower case).
- You can only insert the created html elements or document pieces once. So, you have to call the cloneNode() function.
- The javascript is case-sensitive. So, it should be
cloneNode(true)
, instead ofcloneNode(True)
. - You can turn on the javascript debugger of the browser (both Chrome and IE/Edge) by pressing
F12
. Then you can see what happen to your script.
本文标签: javascriptUsing appendChild multiple times with the same node in JSStack Overflow
版权声明:本文标题:javascript - Using appendChild multiple times with the same node in JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738288010a2073034.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论