Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Insert Links Inside Span Tag Using Javascript

I have this:

<span class="image"><img src="something.jpg"></span>

I want to transform it to that using javascript:

<span class="image"><a href="domain"><img src="something.jpg"></a></span>

It has to be done using javascript in order to hide the affiliate links.

I have tried this script but it seems not to work:

function changespan() {
find all <span> tags;
for each <span> with class="image"{
URL = "http://domain.com"
Create new link to URL;
insert link into <span>;
}       
}

The function is uploaded in file script.js and I load it in this fashion:

<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
window.onload = changespan;
</script>

EDIT: After this is solved, how could i parse my page to find links in this format: and then assign this value to variable URL. I need to be able to assign first path to URL_1 second to URL_2 and so on.

like image 544
Zox Avatar asked Mar 11 '13 13:03

Zox


People also ask

How to dynamically add HTML tags in JavaScript?

The webpage is composed of html tags (elements) one by one, and you can also dynamically add tags such as div, li, and img one by one in javascript. In fact, no matter what html tag, the methods of javascript create element is similar, and then we start by dynamically adding div tag.

How to create a dynamic link span?

click on Create Span link and span will be generate dynamically, If you want more maipulations or doing else then tell me i'll get back to you.. :) The content must be between 30 and 50000 characters.

How to style the <span> tag in HTML?

The <span> tag is easily styled by CSS or manipulated with JavaScript using the class or id attribute. The <span> tag is much like the <div> element, but <div> is a block-level element and <span> is an inline element. The <span> tag also supports the Global Attributes in HTML. The <span> tag also supports the Event Attributes in HTML.

How do I create a span link in word?

click on Create Span link and span will be generate dynamically, If you want more maipulations or doing else then tell me i'll get back to you.. :) The content must be between 30 and 50000 characters. … Download, Vote, Comment, Publish. Forgot your password?


2 Answers

This is how you can implement it:

function changespan() {
    var spans = document.querySelectorAll('span.image');
    for (var i = spans.length; i--; ) {
        var a = document.createElement('a');
        a.href = "http://domain.com";
        spans[i].appendChild(a).appendChild(a.previousSibling);
    }
}

http://jsfiddle.net/Tqv76/1/

like image 51
dfsq Avatar answered Nov 15 '22 01:11

dfsq


Here, I translated it to JavaScript keeping your pseudo code as intact as possible

DEMO

window.onload=function() {
  var spans = document.getElementsByTagName("span"); // or the newer querySelectorAll
  for (var i=0;i<spans.length;i++) {
    if (spans[i].className=="image") {
      var link = document.createElement("a");
      link.href = "http://domain.com";
      link.setAttribute("rel","nofollow");
      link.className="someclass";
      link.innerHTML=spans[i].innerHTML;
      spans[i].replaceChild(link,spans[i].getElementsByTagName('img')[0]);
    }       
  }
}
like image 36
mplungjan Avatar answered Nov 15 '22 01:11

mplungjan