Question

I have a jquery code that adds the tel: link to a style in a span around a phone number but it only works for one phone number. I have a page with 20 different phone numbers and when I add the style to all of the phone numbers it will populate all 20 tel: links with the last phone number in the list to all of the tel: links.

How can I make the tel: link for each phone number populate correctly? Currently It populates just the last phone number in the list to all of the tel: links.

Any help would be much appreciated!

$(document).ready(function() {
    // if Modernizr detects class "touch"
    if($('html').hasClass('touch')) {
        // for each element with class "make-tel-link"
        $(".make-tel-link").each(function () {
        //$.each(".make-tel-link", function () {
            var jPhoneNumber = $(this).text();
            // wrap phone with href="tel:" and then insert phone number
            $(this).wrapInner('<a class="jPhoneLink" href=""></a>');
            $('.jPhoneLink').attr('href', 'tel:'+jPhoneNumber);
        });
    }   
});

Here is an example of the markup.

<!DOCTYPE html>
<html class="touch">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <title></title>



  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css">


  <!-- Extra Codiqa features -->
  <link rel="stylesheet" href="codiqa.ext.css">

  <!-- jQuery and jQuery Mobile -->
  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
  <script src="codiqa.ext.js"></script>
  <script src="modernizr.custom.39046.js"></script>



</head>
<body>
<!-- Home -->
<div data-role="page" id="page2">
    <div data-theme="a" data-role="header">
        <div id="head"> <strong>Contacts</strong></div>
        <div data-role="navbar" data-iconpos="left">
            <ul>
                <li>
                    <a href="index.php" data-transition="fade" data-theme="" data-icon="">
                        home
                    </a>
                </li>
                <li>
                    <a href="3.php" data-transition="fade" data-theme="" data-icon="">
                        contact
                    </a>
                </li>
            </ul>
        </div>
    </div>
    <div data-role="content">

    <h1>CONTACT US</h1>
<div class="layout">
<h4>Headquarters</h4>
4235345 High bar<br />
Suite 345<br />
Quence, AL 45205
<br />
<h4>Customer Service</h4>
info@fgfghgdgh.com<br />
<span  class="make-tel-link">888-555-5555</span><br />
<span  class="make-tel-link">800-555-5555</span><br />
<span  class="make-tel-link">866-555-5555</span><br />

</div><br />
</div>
  </div>
</div>
</body>
</html>
Was it helpful?

Solution

you can create the link first and in there set its attributes and the wrap it

$(function(){
       $.each($(".make-tel-link"), function () {
            //replace all instances of '-'
            var jPhoneNumber = $(this).text().replace(/-/g,'');
            var link = $('<a />', {class: 'jPhoneLink', href: 'tel:'+jPhoneNumber});

           $(this).wrapInner(link);
        });
});

working example

http://jsfiddle.net/pUkUb/3/

Edit:

the problem in your script is here:

$('.jPhoneLink').attr('href', 'tel:'+jPhoneNumber);

you are assigning the attribute to all the elements that have that class, not the one you just created

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top