1

Right i'm trying to insert some basic html before a div like this

<script>$("div#header").before("<div class="test">test</div>");</script>

However nothing appears, but when i try the jquery example

<script>$("p").before("<b>Hello</b>");</script>

it works, what i'm i doing wrong?

Thanks

1
  • 3
    The syntax highlighting in your question should give you a clue. Commented Jun 28, 2010 at 22:24

2 Answers 2

3

You need to escape the quotation marks in the string literal like this:

"<div class=\"test\">test</div>"

Also, all DOM manipulation needs to occur after the browser has finished parsing the HTML document (also called "preparing the DOM"). The browser fires the "load" event for document when the "DOM is ready".

In jQuery, you write a callback for "DOM ready" like:

$(document).ready(function() {
    $("div#header").before("<div class=\"test\">test</div>");
});
Sign up to request clarification or add additional context in comments.

Comments

1

If you're using exactly that code, you should be getting a JavaScript error due to the unterminated string. Try this:

$('#header').before('<div class="test">test</div>');

Comments

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.