For each link where href equals myValue

It may be shorter to write it as:

$('a[href="mywebsite.co.uk"]').each(function() {
    $(this).html("It Worked!");
});

There also is a reason why jQuery has html() function. It cleans up all possible memory leaks and then uses innerHTML internally to set the value you need ;-)


i is the index of the element, you want the element and should use should use something like:

// The first argument is the index, the second is the element
$('a').each(function(index, element) {
    if($(element).attr("href") == "mywebsite.co.uk")
    {
        $(element).html("It Worked!"); // Change this to .html()
    }
    console.log('Index is:'+index+', Element is'+element);
});​

<a href="mywebsite.co.uk"></a>

Also, I changed the .innerHtml() to .html("content in here"). To update the HTML inside the returned <a></a> tag (element).

Check this JSFiddle. http://jsfiddle.net/2scug/1/

Tags:

Html

Jquery