0

I want to break my string using javascript which is fetching some data from a php file using ajax. The php is returning the correct data, however the javascript doesn't treat a <br> tag properly. This is my ajax code and i am using it to replace the current text in that span.

type: "post", url: "some_php.php", data: "name="+name,
    success: function(data) {
      $('#someDiv').text(data);
    }

Using console.log(data) i get the output as:

Some<br>Address<br>to<br>be<br>borken

No i want it to output as

Some
Address
to
be
broken
2
  • do you even know about the <address> tag ?? Commented Feb 25, 2012 at 16:58
  • Yes, thats what i am using inside the div! Commented Feb 25, 2012 at 17:02

3 Answers 3

4

Use .html().

 $('#someDiv').html(data);
Sign up to request clarification or add additional context in comments.

Comments

3

You're using the wrong jQuery function:

$('#someDiv').html(data);

From jQuery's text() function:

.text( textString ) Set the content of each element in the set of matched elements to the specified text.

From jQuery's html() function:

.html( htmlString ) Set the HTML contents of each element in the set of matched elements.

Comments

1
console.log(data.split('<br>').join('\n'));

You can use the previous method to replace all <br>s with newlines.

If you want to have one console entry for each line, use:

console.log.apply(console, data.split('<br>'));

2 Comments

Works fine for the console.log but not for the changing text in the span :S
@Namit Then use .html(), which will also deal with HTML entities.

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.