dazoar dazoar - 1 year ago 60
Javascript Question

Cannot change span text using jquery

I am trying to change the text inside the spans using this piece of jquery but I am unable to. Why does this not work? What is the problem with this piece of code?

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">< /script>
<script>
$(document).ready(function(){

$("span").text("Changed");
});
});

</script>
</head>

<body>

<p>This is a <span>section</span>.</p>

<p>This is <span>another</span> paragraph.</p>

</body>
<html>

Answer Source

Keep eyes on console for errors .. In your code you got an error Uncaught SyntaxError: Unexpected token }",

$(document).ready(function(){

    $("span").text("Changed");
});   
}); //<<<<<<<<<<<<<< remove this line

Working demo

$(document).ready(function(){
  $('span').text('Changed');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>This is a <span>section</span>.</p>
<p>This is <span>another</span> paragraph.</p>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download