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 7632493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:38:24+00:00 2026-05-31T06:38:24+00:00

I made a jQuery script that works fine, I’d just like to see if

  • 0

I made a jQuery script that works fine, I’d just like to see if anyone had tips on simplifying it, in particular the beginning part in which variables are defined.

Though I’m really interested in straight code simplification, here’s a quick synopsis on what the script actually does:

  1. Looks for links with a class of ‘tour’ and defines 3 more variations of its href attribute (swapping out a 4-digit number).
  2. Replaces links with a class of ‘tour’ with different content that substitutes in the additional 4-digit values.
  3. With a.tour replaced, visibility of part of the content is toggled on hover.

And here’s the code:

HTML:

<a href="http://www.awebsite.com/7838" class="tour">Link</a>

JQUERY:

<script>
$(document).ready(function() {
    var aud = $('.tour').attr('href');
    var usd = $('.tour').attr('href').replace(7838,'8062');
    var gbp = $('.tour').attr('href').replace(7838,'8907');
    var eur = $('.tour').attr('href').replace(7838,'8914');
    $('.tour').replaceWith('<div class="currency"><p>Price &amp; Bookings</p><ul class="currencydd" style="display:none"><li><a href="' + aud + '">Australian Dollar (AUD)</a></li><li><a href="' + usd + '">United States Dollar (USD)</a></li><li><a href="' + gbp + '">British Pounds (GBP)</a></li><li><a href="' + eur + '">Euros (EUR)</a></li></ul></div>');
    $('.currency').hover(function () {
  $('.currencydd').slideToggle("fast");
});
});

</script>
  • 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-31T06:38:25+00:00Added an answer on May 31, 2026 at 6:38 am

    Don’t keep using $(".tour") over and over, it is both neater and more efficient to define a variable equal to it. Also, you don’t need to keep checking the .attr("href") because once you’ve stored that value in aud you can use that:

    var $tour = $(".tour"),
        aud = $tour.attr('href'),
        usd = aud.replace(7838,'8062'),
        gbp = aud.replace(7838,'8907'),
        eur = aud.replace(7838,'8914');
    
    $tour.replaceWith(...);
    

    Note that your code will update (replace) all .tour links using the aud, usd, etc. values from the first .tour link. Is that what you intend, or should it update them individually?

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

Sidebar

Related Questions

I've made jQuery & AJAX script that uses a PHP function to bring back
I have made a image gallery with jQuery found here: http://sarfraznawaz2005.kodingen.com/demos/jquery/image_gallery/ I just wanted
I have started using jQuery and rails. I have made a simple form that
I'm using jQuery and made a plugin for some in house work that basically
I am working on a site here: mfm.treethink.net All the jquery works fine in
I made a small script which basically uses jQuery uplodify to upload files to
I'm having some issues with a jQuery AJAX call. My code works fine when
I'm using jsdom with jquery, and it's working just fine. However, I'm trying to
I have an issue with a jQuery script I wrote that is driving me
It works on Chrome and every other browser, just not IE, I've made an

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.