Himmators
Himmators

Reputation: 15006

How to get jquery to find href that is phone number?

I'm trying to get all callable phone numbers out of an html-received with a get:

onload: function (data)
{
    data = $.parseHTML(data.response);
    var content = $.trim($(data).find('[href^=callto:]').text());
    console.log(content)
    //var content= $(data).find('.');
}

The data is correct, i successfully found find('.tel'), a class used in the html.

Upvotes: 2

Views: 7808

Answers (3)

Petko Bossakov
Petko Bossakov

Reputation: 540

The colon is a special character in jQuery selectors. You should escape it like this: $(data).find('[href^=callto\\:]')

Upvotes: 1

bstojanovski
bstojanovski

Reputation: 83

This is how I managed to resolve this problem on my end. :)

/* All phone numbers to href */
var regex = /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/;
$('tr td:nth-child(3)').each(function() {
    var text = $(this).html();
    text = text.replace(regex, "<a href=\"tel:$&\">$&</a>");
    $(this).html(text);
});

Upvotes: 1

Andr&#233; Dion
Andr&#233; Dion

Reputation: 21708

$('a[href^="tel:"]') will give you all anchors with a tel: scheme.

Using your sample code: data.find('a[href^="tel:"]')

Upvotes: 8

Related Questions