Reputation:
There is some problem with my js script Its executing perfectly but at browser is in running stage like in infinite look. Browser is not stopping.
IS there any issue with this code
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<p MyTag="home_id">home</p>
<p>33333333333</p>
<p MyTag="content_id">Content</p>
<script>
$(document).ready(function(){
var a = 'New text';
$('[MyTag]').each(function(index) {
document.write(index + ': ' + a + "<br>");
});
});
</script>
</body>
</html>
Upvotes: 0
Views: 88
Reputation: 2377
To change value in every tag you can use below.
$(document).ready(function(){
var a = 'New text';
$('[MyTag]').each(function(index) {
$(this).html(index+':'+a);
$(this).val(index+':'+a);
});
});
html(): In an HTML document, .html() can be used to get/set the contents of any element.
val() : Get the current value of the first element in the set of matched elements or set the value of every matched element.
Upvotes: 2
Reputation: 15629
Don't use document.write, or you have to often handle such problems. If you want to append some content to such elements, use the append
-method from jQuery
$('.element').append($("<span />").text("your text.."));
Upvotes: 0
Reputation: 842
try this
$(document).ready(function(){
var a = 'New text';
$('[MyTag]').each(function(index) {
$(this).html(index+':'+a);
});
});
good luck
Upvotes: 0