Search code examples
javascriptjqueryhyperlinkhref

Change href to tel: + number (user defined)


I'm trying to write a script where users can type a phone number and store it in the input (which works in this demo: http://bootsnipp.com/snippets/featured/iphone-number-pad) and then update href="" = "tel:" + that-same-value. So for example the if the user types 600 999 999 the href is updated to href="tel:600 999 999" and the user can then click the the button and make a call.

I've been beating myself up trying to figure this out. It seemed really simple because I'm not even trying to use restrict the phone number to 6-7 characters long.

Any help would be greatly appreciated.

   $(document).ready(function() {
     // Get number
     $('.num').click(function() {
       var num = $(this);
       var text = $.trim(num.find('.txt').clone().children().remove().end().text());
       var telNumber = $('#telNumber');
       $(telNumber).val(telNumber.val() + text);
       $('#call-this').href = "tel:" + $(telNumber).val(telNumber.val() + text)

       console.log(text);
       console.log(telNumber);
     });
     // add number to href
     var call = $(#call - this).attr('target')
       // Other stuff I've tired
       //   $('#call-this').click(
       //   $('#call-this').href="tel:" + $(telNumber).val(telNumber.val() + text)
       //    var call = $('#call-this');
       //    $('#call-this').href=  "tel:" + telNumber;
       //    console.log(call);
       // });

   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="tel" name="name" id="telNumber" class="form-control tel" value="" />
<div class="num">
  <div class="txt">0</div>
</div>
<div class="num">
  <div class="txt">1</div>
</div>
<div class="num">
  <div class="txt">2</div>
</div>

<div class="num">
  <div class="txt">3</div>
</div>
<div class="num">
  <div class="txt">4</div>
</div>
<div class="num">
  <div class="txt">5</div>
</div>

<div class="num">
  <div class="txt">6</div>
</div>
<div class="num">
  <div class="txt">7</div>
</div>
<div class="num">
  <div class="txt">8</div>
</div>
<div class="num">
  <div class="txt">9</div>
</div>
<button class="expand">
  <a href="#" id="call-this" style="color:black;">Call</a>
</button>


Solution

  • Your function can actually be simplified into this — the reason is that you don't need to actually transverse all the way to the .txt element if it is the one and only child of .num and that it only contains the number of interest.

    $(document).ready(function () {
        $('.num').click(function () {
            // Append trimmed number to current value
            $(telNumber).val($(telNumber).val() + $(this).text().trim());
    
            // Update href attribute on #call-this
            $('#call-this').attr('href', 'tel:'+$(telNumber).val());
        });
    });
    

    See fiddle here: http://jsfiddle.net/teddyrised/jwqL4o8w/3


    In fact, a better choice would be to store the number in the HTML5 data- attribute, so you can freely change the innerHTML of the .txt element without having to worry about issues:

    <div class="num" data-num="0">
        <div class="txt">0</div>
    </div>
    

    Then for your JS, just use:

    $(document).ready(function () {
        $('.num').click(function () {
            // Append trimmed number to current value
            $(telNumber).val($(telNumber).val() + $(this).data('num'));
    
            // Update href attribute on #call-this
            $('#call-this').attr('href', 'tel:'+$(telNumber).val());
        });
    });