1

I have a script that reads a list of addresses from a table and trace the route on google maps.

(Similar to my old one: how to use the google maps api with greasemonkey to read a table of addresses and trace the route?)

But there is a "bullet" symbol in front of every address, and google maps wont remove it to render the map, giving me the error below

enter image description here

this is the HTML for the table cell

enter image description here

Any hints on how to remove the "bullet" with JQuery/Javascript?

Community
  • 1
  • 1
RASG
  • 5,988
  • 4
  • 26
  • 47
  • 1
    the most efficient way is to change the code that reads the data. Can you give us a sample of that? Otherwise, using JS to fix it at a later stage can be problematic. – ericosg Jun 18 '12 at 11:53
  • Have you tried replace method? http://www.w3schools.com/jsref/jsref_replace.asp – Michal B. Jun 18 '12 at 11:53
  • 2
    If its always there, chop it off? `"• bleep bloop".substr(2)` – Alex K. Jun 18 '12 at 11:55
  • Im not sure that it will always be there. Isnt there an ASCII code or something similar? – RASG Jun 18 '12 at 11:57
  • Paste the actual source HTML with the "bullet" in it -- or better yet, link to the whole page. There's more than one way to make a "bullet". – Brock Adams Jun 18 '12 at 12:17

3 Answers3

2

If you don't want to .substr(2) you can replace the 1st occurrence with:

var s = $("#para").text().replace("\u2022", "");

Where \u2022 is the character for the html bullet entity •

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • This could work but it's not a general approach as the replace works in allt he string (ok, I know it's very unlikely to have bullets INSIDE the string, but this problem should be addressed in a general manner). – Cranio Jun 18 '12 at 12:07
  • 1
    It works for the 1st occurrence only, subsequent ones will be ignored & it wont arbitrarily chop off the 1st 2 characters if the char is not present. If you wanted to anchor it to the start you could use `/^\u2022\s/` – Alex K. Jun 18 '12 at 12:09
  • @Cranio Probably. And feel free to correct me if im wrong (im not very good with regex), but cant i just add `/g` to do a global replace? – RASG Jun 18 '12 at 17:36
  • No, because the "global" part still refers to a single string ("global" means "replace all the occurrences *in the string*"). In other words, regex has no awareness of an "object/element collection". – Cranio Jun 18 '12 at 17:37
  • @Cranio by "global" i meant in the string (the `var s` int this example). like 1 bullet at the beginning, and another one at the end. – RASG Jun 18 '12 at 17:42
  • If so, then it's ok. But still it works only for the first occurrence. – Cranio Jun 18 '12 at 17:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12709/discussion-between-rasg-and-cranio) – RASG Jun 18 '12 at 18:07
  • To replace them all; `var s = $("#para").text().replace(/\u25AA/g, "");` – Alex K. Jun 19 '12 at 09:46
1
$(".dadosf").each(   
 function(i,e)
 {
     $(e).text($(e).text().substring(2));
 }
);

​ JQuery's .each() iterates through the collection, we simply replace the inner text with the same text taken from the 2nd character (substring(2))


This approach, as another user pointed out, works if you are sure that there is a bullet. If not I'd use a conditional in the code instead of :contains(text) selector, to have a general approach and make sure we strip only the character at the beginning (it's unlikely to have a bullet INSIDE the text, but :contains() would match also strings with this condition, which is wrong):
$(".dadosf").each(   
 function(i,e)
 {
     var t = $(e).text();
     if (t.substring(0,1) == "\u2022")
         $(e).text(t.substring(2));
 }
);

Cranio
  • 9,647
  • 4
  • 35
  • 55
1
$(".dadosf:contains('.')").each(

 function()
 {
     $(this).text($(this).text().substring(2));
 }

);
Raab
  • 34,778
  • 4
  • 50
  • 65
  • The selector is not ok if the bullet is INSIDE the string. – Cranio Jun 18 '12 at 12:16
  • That's not the point. Solutions should be reusable and account for exceptions and quirks: what if Google Maps, on which the OP has no control, returns a bullet INSIDE the string?. Although it might work in *this* case, you cannot draw a general conclusion from a snapshot. – Cranio Jun 18 '12 at 12:36