Reputation: 431
I'm using Google Maps API geocoding to turn an address into a location on the map. I have it set up as:
var address = document.getElementById("address").innerText;
I have my address div set up as
123 candycane lane (br /> north pole, antartica
Whenver I put a
after the first line, the geocoding won't return any results. But when I remove it and have the address as one line, the geocoder works perfectly. I had thought innerText did not account for HTML such as (br />. Is there anyway to make it so it skips over the line break?
Thanks
P.S. I know my line breaks have parantheses at the beginning, but its because my post wasnt recognizing the HTML line break.
Upvotes: 1
Views: 877
Reputation: 29381
.innerText does not return a linebreak-tag (br), as you say, but it returns a newline character (\n).
Simple solution (replace it with a blank space):
var address = document.getElementById('address').innerText.replace(/\n/g, ' ');
Edit, .replace() is a method on the string object, which takes a regular expression as a first parameter. The 'g' stands for global, which means it should replace all occurrences of \n. Without it, only the first occurrence would be replaced. The second parameter is the substitute string.
Upvotes: 2