2

I have a String variable say

strHTML = "<div class='abc'> This is a test <span class='xyz'> String </span> </div> "

that i m receiving from server. When I use

$('#container').append(strHTML);

it is displaying the whole string including HTML tag in "container".

What i want is to take each HTML tag as HTML element in "Container" & apply class whever i defined in it..

2
  • I did this working... Actually wat i do is.. var str = $(this).children().eq(0).text(); $(this).children().eq(0).html(str); Commented Mar 20, 2012 at 7:42
  • 1
    your code seems to work: jsfiddle.net/robasta/7MgeA What do you mean by 'apply class wherever I defined it'? Commented Mar 20, 2012 at 7:45

5 Answers 5

2

Split your string and change them into DOM Object. Then you can manipulate them as you want.

Example:

var abcDiv = $("<div></div>");
abcDiv.attr('class', 'abc'); // add class
var xyzSpan= $("<span></span>");
xyzSpan.attr('class', 'xyz'); //add class

abcDiv.append("This is a test"); //Add the text
abcDiv.append(xyzSpan); //Add the span

$('#container').append(abcDiv); //insert the div
Sign up to request clarification or add additional context in comments.

1 Comment

i have a lots of DOM objects in string.. so ur sugession is not gud in this case.
2

I did this working... Actually, what i did is..

var strHTML = "<div class='abc'> This is a test <span class='xyz'> String </span> </div> ";
$("#container").html(strHTML);

Thankx to all for helping me..

Comments

0

Try

$('#container').append($(strHTML));

Comments

0

You are using single quotes for the class attribute. This is incorrect.

You need to do it this way:

$(document).ready(function(){
var strhtml = '<div class="abc"> This is a test <span class="xyz"> String </span> </div>';
$('#container').append(strhtml);    
});
​

Live Demo

Note the difference in strhtml. I used single quotes to wrap the entire string, while using double quotes for the value of the class attributes.

3 Comments

Wait, what? It doesn’t make a difference whether you use single or double quotes around attribute values in HTML. Just make sure to escape any quotes matching the surrounding quotes inside the string.
It probably doesn't but the W3C specification for HTML4 states that values for id and class attributes should be enclosed in double quotes. Browsers will probably handle single quotes as well, but that's not a good reason to use single quotes. [w3.org/TR/REC-html40/struct/global.html#adef-id]
The link you point to doesn’t say that — it just happens to provide some examples where double quotes are used.
0

There is nothing wrong with your code. Are you sure the string really comes formatted like that from the server? Or have the < > characters perhaps been substituted for &lt and &gt?

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.