How to wrap a link around a div jQuery?

user4061223

The code below should a user to click on a button of which he is prompted for a URL. Once a URL is entered, a new i element is created with a link to the inputted URL wrapped around it as an a tag.

The code works however the link tag isn't created - how do I go about doing this?

The new link should be wrapped around that specific div only.

Thanks.

<script type="text/javascript">
    $(function() {
      $(".twitter_new").click(function() {
          link = prompt("Paste a url");
          if (link == null) {
          return;
          }
          else {
          div = document.createElement('i');
          $(div).addClass("fa").addClass("fa-twitter").wrapAll('<a href="' + link + '"></a>');
          $(".icons").append(div);
          }
        });
    });
</script>
charlietfl

Your wrapAll() expression doesn't modify what the variable div is. It returns a new object.

You are still appending the original i element only

I would suggest you create 2 elements or do this all as html string

// create `<i>`
var $i = $('<i>',{class: 'fa fa-twitter'});
// create <a> and append <i>
var $a = $('<a>', {href:link}).append($i);
// append complete <a> to dom
$(".icons").append($a);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how wrap div around two element with jquery

From Dev

how wrap div around two element with jquery

From Dev

Wrap <a> around <div> without jQuery

From Dev

How to wrap div around image?

From Dev

Wrap a span around link text with jQuery

From Dev

JQuery, wrap div around every single div

From Dev

jQuery wrap a div around a massive block of HTML

From Dev

Wrap div around 2 sequential elements in jQuery

From Dev

jQuery wrap a div around a massive block of HTML

From Dev

Wrap a div around every 2 elements in jQuery

From Dev

How to wrap text around image in div?

From Dev

How to wrap div around <p> tag?

From Dev

How to wrap a div containing text around a div containing an image?

From Dev

Wrap div around a hidden div

From Dev

How to wrap an element with a div in jQuery?

From Dev

How to overlap image over the div, but still wrap text around an image?

From Dev

How to "shrink wrap" a div around it's floated children

From Dev

How to overlap image over the div, but still wrap text around an image?

From Dev

How to "shrink wrap" a div around it's floated children

From Dev

How can I wrap a div around existing elements?

From Dev

How do I wrap an a tag around a div tag?

From Dev

wrap div around other divs

From Dev

Wrap text around a div in html

From Dev

Wrap a div around every iframe

From Dev

Wrap a div around every iframe

From Dev

How to wrap single qoute(')around variables inside jQuery html() method

From Dev

jQuery .wrap() - specify position to wrap around

From Dev

jQuery wrap only around string

From Dev

Better way to wrap a link around an element in Javascript