2
$("p").click(function(){
$(".Part1 p").html("some new text that will replace what <br /> was in the paragraph.   <p> and a paragraph </p>");
});

Edit: added ); to the end for clarity.

When I try to put html tags in my strings it either doesn't show up at all as is the case of <br /> or it breaks the script and I get an error as is the case of </p>

How can I added html tags to strings so that I can have my text formatted the way I want but still make dynamic changes?

3
  • That should work perfectly. Can you post a link to a live demo? Commented Jun 6, 2011 at 20:07
  • That's a good idea I should have thought of that first. Let me see if I can get it up somewhere. Speaking of which does anyone know a convenient place to host demo files for a short time? Commented Jun 6, 2011 at 21:10
  • Strangely, it works on jsfiddle and in the actual file =/ Commented Jun 6, 2011 at 21:18

4 Answers 4

4

The <br /> shouldn't be a problem, but a <p> element cannot be a child of another <p> element, so you are trying to construct an invalid DOM (and so errors are to be expected).

Sign up to request clarification or add additional context in comments.

1 Comment

I see what you are saying even when I do it with <span> tags it does the same thing (it actually deletes the text that is found within the tags). I'm using TextWrangler and when in the string it gets to '</' I lose all my code coloring for the rest of the <script>
1

Works for me : http://jsfiddle.net/D6XCv/

But maybe you forgot to close the ")" for the click() event :

$("p").click(function() {
    $(".Part1 p").html("some new text that will replace what <br /> was in the paragraph.   <p> and a paragraph </p>");
}); // <== this ")" !

3 Comments

Thanks for the site actually I just grabbed that line from a larger piece of code that I had written, in that one the i closed it correctly sorry for the confusion
When I tested it on that site it worked as well. Really strange that it works in one place and not another.
That means the problem is from somewhere else.
0

When I tested this in a browser, I needed to append a trailing );

$("p").click(function(){
$(this).html("some new text that will replace what <br /> was in the paragraph.
    <p> and a paragraph </p>");
});

1 Comment

I just grabbed that line from a larger piece of code that I had written, in that one the I closed it correctly sorry for the confusion.
0

I'm using TextWrangler and when in the string it gets to '<script>

The sequence </ will end the parsing of a script element under SGML (and thus HTML 4) rules (although it isn't respected so much by many browsers).

Either:

  1. Use an external script file and src it or
  2. Escape the slash character: <\/

1 Comment

When I used <\/ my code highlighting came back on but then when I tried to run it in the browser I got this error. error on line 17 at column 144: StartTag: invalid element name

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.