Michael Robinson
Michael Robinson

Reputation: 1132

Changing div content using jQuery

I have this code, I want to modify #spock content using jQuery :

<script type="text/JavaScript">
$(document).ready(function () {
    var url = window.location.href,
    server = "Testing123",
    content = "",
    background = "";
    content = '<p class="server-message ' + server + '">You are on the <strong>' + server + '</strong> server</p>';
    alert('content = ' + content);
    $("#spock").text(content);
});
</script>
<div id="#spock"></div>

I expect it to display "Testing123" on my page, yet I see nothing. I'm not sure what I am doing wrong.

Upvotes: 1

Views: 59

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

Firstly, the id of the element doesn't need the # in it:

<div id="spock"></div>

Secondly, you're adding HTML to the element so using text() will encode any HTML tags in the string. Use the html() function instead:

$("#spock").html(content);

Working fiddle

Upvotes: 4

Related Questions