1

I have the following html in a footer div:

 <span itemprop="telephone"><a href="tel:01 XX XX XX XX">01 XX XX XX XX</a></span>

I want to dynamically change the phone number within this span. 2 issues there:

  • span is using microformats data so I cannot based my find on itemprop but I need to base it by using telephone I guess?

  • I can udpate 01 XX XX XX XX embedded between the tags but what about the href value?

Anyone could help?

I've tried this which is not working

    function getPhone() {
        var span = $('.wcb_row2');
        if (!span.length) return;
        return $('.wcb_row2').find('a');
    }

    function replacenumber(num) {
        if( num == 'undefined' || num== null )
        return;
        var newnum = getPhone();
        (newnum.html( newnum.html().replace(/tel:*[ ]*\d{2}[ ]\d{2}[ ]\d{2}[ ]\d{2}[ ]\d{2}/, 'tel: ' + num) ))
    }

    replacenumber( num);

1 Answer 1

3

Basically you're not changing span content but manipulating anchor a within it...

Have you tried doing something like this?

var yourNumber = /* whatever needs to be */
$("a", "[itemprop=telephone]")
    .text(yourNumber)
    .attr("href", "tel:" + yourNumber);

General advice - learn what you use

If I could give you a general advice it would be to learn. It's pretty obvious you're unfamiliar with all jQuery functionality as well as weak on regular expressions.

I would therefore suggest you go through jQuery API. It's a small library and you should get through it in about an hour or so. YOu should get acquainted with all the functions it has so you'd at least know whether there's something to use or not. You can read their usage details later when you need to, but you should at least know about them that they're there.

Don't get me wrong. I'm trying to help you because it will make you more productive and you'll be able to write more maintainable code in the future.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.