Reputation: 2711
I would like to do a bookmarklet which obtains some information from a certain website.
After using $('#div').text();
and avoiding the html elements that are inside the div, i get the following string:
TOP-GOST d.o.o. Tel: 01 200 50 80 , 041 843 303 E-mail: [email protected] Spletna stran podjetja Tbilisijska ulica 59 1000 Ljubljana Slovenija
As you can see there is alot of unnecessary spaces and different information inside one big string. What i would like to do is to remove all spaces, remove unnecessary information (as Tel:, E-mail:, Spletna stran podjetja) and separate important information with comma ','
.
Is it possible to put every independent part of information into its own variable? My solution would be something similar to php's explode()
or inverted javascript join()
, after pieces are glued together with a comma.
About removing the unnecessary parts, is using a .replace().
good idea?
Desired result:
variable one_string = 'TOP-GOST d.o.o., 012005080, 041843303, [email protected], Tbilisijska u...';
AND LATER
variable title = 'TOP-GOST d.o.o.'
variable phone = '012005080,041843303'
variable email = '[email protected]'
etc.
Original source code HTML:
<div class="offer-contact">
<h3 class="offer-company">
TOP-GOST d.o.o.</h3>
<strong>
Tel:
</strong>
01 200 50 80 , 041 843 303<br>
<strong>
E-mail:</strong> <a href="mailto:[email protected]">
[email protected]</a><br>
<strong>
<a href="http://www.via-bona.com" target="_blank">Spletna stran podjetja</a><br>
</strong></div><strong>
<div class="offer-map">
<p>
Tbilisijska ulica 59<br>
1000 Ljubljana <br>
Slovenija<br>
</p>
</div>
Upvotes: 1
Views: 158
Reputation: 198324
$('#div').text();
and avoiding the html elements that are inside the div
Why aren't you using the HTML structure instead of discarding it? Instead of accessing $('#div')
, why not access $('#phone')
, $('#email')
... separately? If they don't have ID, but have stable structure, you can use $('#div > div:nth-child(3)')
kind of selectors to pinpoint what you're looking for.
EDIT: Now that we can see the structure:
var title = $('.offer-company').text().trim();
var email = $('.offer-contact a').attr('href').trim();
var address_array = $.map($('.offer-map p').html().split('<br>'), function(v) {
var t = v.trim();
if (t.length) return t;
});
// Phone is trickier; it's not in a tag by itself. So, this
// is the more reliable method (get rid of everything else):
var $offer = $('.offer_contact').clone();
$offer.find('.offer-company, strong, br, a').remove()
var phone_array = $.map($offer.html().split(','), function(v) {
var t = v.trim();
if (t.length) return t;
});
// The alternative would have been to go with regexp, which
// is not recommended for cutting up HTML.
Something like this should do it. If you need the comma-separated string of address or phones, you can do address_array.join(', ')
(same for phones).
Upvotes: 3