views:

837

answers:

1

JQuery is not being able to identify tags when I use in the Master Page. The following code:

<script type="text/javascript">
    $("body").append('<div id="test"><p>Hello</p></div>');
</script>

Works fine in normal pages, but when the body is in the master page and I put this same code in the Master page - nothing happens!

How can I append to the page body from the ASP master page? Is there some sort of trick to this?

Any help would be greatly appreciated.

Mark.

+5  A: 

looks like you need to wrap this in a document.ready block that way it will happen when the page is ready.

Remember the ASP.NET page lifecycle (refer here for a reference). I think this worked in your base page and not in the master page because the page happened to be ready once the base page loads, not once the master page loads.

<script type="text/javascript">
    $(function() {
        $("body").append('<div id="test"><p>Hello</p></div>');
    });
</script>

which is also the same as

<script type="text/javascript">
    $(document).ready(function() {
        $("body").append('<div id="test"><p>Hello</p></div>');
    });
</script>
Jon Erickson
Doh. I haven't had coffee today. Thanks Jon.
Mark Kadlec