3

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.

1
  • When is this code being executed when you have it placed on the master page? Appending to the DOM before it is completely loaded would cause issues. Commented Mar 5, 2009 at 21:30

1 Answer 1

6

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Doh. I haven't had coffee today. Thanks Jon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.