0

I am accessing links on a webpage with jQuery, with the following code:

$('a[href]').each(function() {
  $(this).attr('class', 'visited');
  $(this).attr('href', '#');
})

The class on the link will be changed, but the href will not. Is there something preventing me from changing/altering the href?

Edit:

I updated the code to the following:

$('a[href]').each(function() {
        $(this).addClass('visited');
        this.href = '#';
        })

However, although it works on MOST websites, it doesn't work on news.yahoo.com. Any reasons why this is so?

4
  • 3
    Seems to work for me: jsfiddle.net/PbC8y Commented May 2, 2013 at 18:35
  • This script does work on most websites: However, for example, it doesn't seem to work on news.yahoo.com. I am using the updated code per @PSCoder: this.href ="#" Commented May 2, 2013 at 19:10
  • are you trying to access news.yahoo.com elements via an iframe ? this is not supposed to work Commented May 2, 2013 at 19:36
  • This is not supposed to work, for an even greater reason - news.yahoo.com does NOT use jQuery Commented May 3, 2013 at 17:42

2 Answers 2

3

For the href, you probably want to use .prop() instead of .attr().

For the classname, in most cases you want to use .addClass() instead of overwriting the entire class attribute.

Sign up to request clarification or add additional context in comments.

4 Comments

Why use .prop()? This isn't something that users normally alter like checked or selected. .attr() is entirely correct to use in this case.
@Blazemonger - I think you are right - I stand corrected. So would it be accurate to say that either .prop() or .attr() is equally valid for this use? (Actually I like the direct DOM property reference in PSCoder's answer the best.)
It's possible to change the checked state of a checkbox input without altering the DOM -- you just click on the checkbox. In that case, .prop() is the correct method. However, it's impossible to change the href of an anchor without modifying the DOM directly in some way, so .attr() is the appropriate method.
Ideally, prop should not be used as you are not changing/toggling a property. You are modifying the class which is an attribute and hence attr()
3

You don't need to use jquery wrapper over this to do this:- You can just access href as a property from this itself as it represents the dom element.

$('a[href]').each(function() {
 ...
   this.href ="#";
})

3 Comments

But $ itself is jQuery and each a memberfunction of jQuery ;)
What he means is that you don't need to use jQuery for the line of code that sets the href; the direct DOM reference is as easy or easier to use. Of course you're still using jQuery for other parts of your code.
@MichaelGeary Thanks for clarifying to OlikerK. I edited my answer to make it more clear. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.