Extract text using javascript from within a div?

Would someone please tell me how to extract the street address, city, state, and postal code from within this HTML div using javascript into 4 separate variables?

<div class="address">
123. Broadway
<br>
New York, NY  10009
</div>

var streetAddress = "";
var city = "";
etc, etc.

Thanks in advance for any help
LVL 1
Westside2004Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
test page : https://jsfiddle.net/c9grsf4u/
jQuery(function($) {
	var addresses = []; // we store all in an array
	$(".address").each(function(i,v) {
  	var html = $(this).html().replace(/\n|\r/g, "");
  	var twoLines = html.split("<br>");
    var line1 = twoLines[0];
    var line2 = twoLines[1];
    var cityandpc = line2.split(",");
		var streetAddress = line1;
		var city = cityandpc[0];
    var postalcode = cityandpc[1].split(" ")[3];
    var address = {
    	"streetAddress": streetAddress,
      "city": city,
      "postalcode" : postalcode
    };
    addresses.push(address);
	});
  $("#ta").val(JSON.stringify(addresses)); // for testing we display the array in a textarea
});

Open in new window

0
Leonidas DosasCommented:
<!DOCTYPE html>

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div class="address">123. Broadway 
<br>
New York, NY  10009
</div>
        
        <script>
            var elm=document.getElementsByClassName('address')[0].innerHTML;
            
            elm=elm.replace('<br>','');
            elm=elm.split(' ');            
            var street=elm[0]+elm[1];
            var city=elm[2].replace(/\s/g, '')+elm[3].replace(',','');            
            var cityShort=elm[4];
            var postalCode=elm[6];
            console.log(street);
            console.log(city);
            console.log(cityShort);
            console.log(postalCode);
            
        </script>
    </body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.