2
<div class="faddress">
    1 myroad valley<br>beverly, VT&nbsp;54803-0989<br>(435) 425-9090 <br><br>
    <a href="someline.html" target="_blank" title="RBP">RBP</a>
    <br><span>(435) 425-9090 </span>
    <div class="mE">1 myroad valley&nbsp;beverly, VT&nbsp;54803-0989</div>
</div>

I am trying to get only 1 myroad valley and I used the following JS:

var vAdd = $(".mE").text();
var vArr = vAdd.split("&nbsp;");
console.log(vArr[0]);

I keep getting the following: 1 myroad valley beverly, VT 54803-0989

How can I modify the code to get the desired result.

3
  • Why you didnt use substring ? Commented May 18, 2015 at 13:40
  • Have a look at the value of vAdd at runtime. Then it should be easy to find the right split character. Commented May 18, 2015 at 13:42
  • 2
    use html() instead of text() Commented May 18, 2015 at 13:43

3 Answers 3

5

Use .html instead of .text:

var vAdd = $(".mE").html();
var vArr = vAdd.split("&nbsp;");
alert(vArr[0]);

.text function get only text and ignore HTML. That's why .text not getting &nbsp; and you are getting full text.

DEMO.

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

1 Comment

@SearchForKnowledge My pleasure to help you. :)
5

Use the html method instead of the text and your code will work.

> $(".mE").html();
> "1 myroad valley&nbsp;beverly, VT&nbsp;54803-0989"
> $(".mE").text();
> "1 myroad valley beverly, VT 54803-0989"

Comments

1

Here is a javascript only solution,No need jQuery

var vAdd = document.getElementsByClassName('mE')[0].innerHTML;
var vArr = vAdd.split("&nbsp;");
console.log(vArr[0]);

DEMO

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.