user3165155
user3165155

Reputation:

Custom attribute change content using jquery

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

Answers (3)

Praveen D
Praveen D

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

Philipp
Philipp

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

saman khademi
saman khademi

Reputation: 842

try this

    $(document).ready(function(){
       var a = 'New text';
       $('[MyTag]').each(function(index) {
         $(this).html(index+':'+a);
       });
    });

good luck

Upvotes: 0

Related Questions