Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 393911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:18:16+00:00 2026-05-12T16:18:16+00:00

Im finding jQuery to difficult to learn as there seems to be many ways

  • 0

Im finding jQuery to difficult to learn as there seems to be many ways to write the same thing.

As an exercise I would like to take the text within anchor tags and stuff it into the links href attribute.

eg

<a href="">http://www.something.com</a>

to become

<a href="http://www.something.com">http://www.something.com</a>  

my first attemp was

<script type="text/javascript">
$(document).ready(function() {
var text = $('a').text();

$('a').attr( 'href', text );
});
</script>

which clearly doesnt work as I need to specify to do this action for each link.

Should I use a foreach loop? A .each() function? Or $this notation?
Would they all work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:18:16+00:00Added an answer on May 12, 2026 at 4:18 pm
    $('a').text()
    

    would return the combination of every anchor nodes text value.. I just did it on stackoverflow and it gave me:

    “mederlogoutaboutfaqQuestionsTagsUsersBadgesUnansweredAsk
    QuestionJquery $this or each() to
    specify one link at a
    timejqueryeachthiseditcloseflagchrisadd
    commentjqueryeachthisask your own
    questionjquerythiseachC++/Unix
    Programmer at Waterfront International
    Ltdjobs.stackoverflow.comquestion
    feedaboutfaqblogpodcastprivacy
    policyadvertising infocontact
    usfeedback always
    welcomestackoverflow.comserverfault.comsuperuser.commetahowtogeek.comdoctype.comcc-wikiattribution
    required”

    Therefore you should use jQuery.prototype.each so you can save a reference to each individual anchor’s text:

    $('a').each(function() {
        var text = $(this).text();
        $(this).attr('href', text );
    });
    

    The added benefit is that each function runs in its own execution context, each variable you define, every ‘text’ declared is unique to that function body so there’s a lot more control inside of a .each.

    Regarding the vanilla loop deal – I would use jQuery.prototype.each over it because it’s a higher level function, you would have to assign a variable to the length of the nodeList and start at 0, stop at the length ( or if order doesn’t matter do a reverse while loop ).. this just causes more work than necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a resource for finding the best jquery plugins, or at least just
I have a jquery game that my members keep finding ways to win. It's
I'm having trouble finding an open-source IDE with support for Git. Are there any
I'm using jquery and googlemaps (gmap2). I am finding issues with using the jquery
Hello I am writing some jquery plugins, i read some tutorials finding all clear.
Finding a good way to do this has stumped me for a while now:
I'm failing at finding the commands I need to send to authenticate to a
I'm finding that I can't access the admin shares on an XP64 box when
I'm interested in finding out why this is used on some Web sites for
I'm finding myself doing a lot of things with associative arrays in PHP. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.