views:

799

answers:

2

Hi, on a page there a several links of:

<a class="linked" href="http://link1.com&gt;http://link1.com&lt;/a&gt;

<a class="linked" href="http://link2.com&gt;http://link2.com&lt;/a&gt;

How would one remove the second http:// in each link so it can't be seen on the screen.

I've tried this to no avail:

$(document).ready(function() {

$('.linked').html().replace("http://","");
+3  A: 

If you are talking about the visible text in an anchor tag

$.each($('.linked'), function()
{
  var anchor = $(this);
  anchor.text( anchor.text().replace("http:\/\/",'') )
}
james
Why `$.each($('.linked'), ...` instead of `$('.linked').each(...`?
Christoph
They're equivalent -- personal preference.
thenduks
The way I wrote it works with other kinds of collections, not just jquery collections. So I tend to write them that way.
james
Neither of these had the desired effect, did nothing: $('.linked').each(function() { var anchor = $(this); anchor.text( anchor.text().replace("http:\/\/",'') );});$.each($('.linked'), function(){ var anchor = $(this); anchor.text( anchor.text().replace("http:\/\/",'') )});
Adrian33
A: 

Just for the record, the jQuery-less version:

var links = document.links;
for(var i = links.length; i--; ) {
    with(links[i]) {
        if(/(^|\s)linked(\s|$)/.test(className)) {
            firstChild.nodeValue =
                firstChild.nodeValue.replace(/^http:\/\//, '');
        }
    }
}
Christoph
okay, that didn't work either when placed in the head or body.
Adrian33
@adrian33: the links must exist before you can modify them, ie you have to put the script element as last element of the body, use jQuery's `ready()` function, `window.onload` or `DOMContentLoaded`
Christoph