Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically add anchor/href in jQuery

Tags:

jquery

Im new to jQuery and i'm trying to convert all my phone class to have an anchor with an href:

What I have

<div class="phone">111-111-1111</div>
<div class="phone">666-555-4444</div>

What I want

<div class="phone"><a href="tel:111-111-1111">111-111-1111</a></div>
<div class="phone"><a href="tel:666-555-4444">666-555-4444</a></div>

I am trying to do something like this, but I am pretty lost:

$('.phone').each(function(){
  $(this).wrapInner('<a name="???' + $(this).html() + '" />');
});
like image 476
thunderousNinja Avatar asked Sep 18 '12 04:09

thunderousNinja


People also ask

How can I add href attribute to a link dynamically using JavaScript?

To add href attribute to a link dynamically using JavaScript, we can set the link's href property. const a = document. getElementById("yourlinkId"); a.

How to use anchor tag in jQuery?

To add a title in anchor tag in jQuery, use the prop() method. The prop() method is used to set properties and values of the selected elements.

How do you make a dynamic href in HTML?

I have a <span id="aa"> to enclose a link tag with href="#" as the url.


1 Answers

I think solution is there in your question only...

Have a look at this.

$('.phone').each(function(){
    $(this).wrapInner('<a href="tel:' + $(this).html() + '" />');
});​

FIDDLE Hope this is what you want.

like image 65
Vins Avatar answered Oct 25 '22 15:10

Vins