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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:35:22+00:00 2026-05-29T06:35:22+00:00

http://jsfiddle.net/9BCrs/5/ I have this set up to load a file into a DIV using

  • 0

http://jsfiddle.net/9BCrs/5/

I have this set up to load a file into a DIV using the two links in the left DIV but it requires a unique copy of the same JQuery code each time there is a new link.

Is there a way of connecting the file being called and the DIV it is being called from to the link via some kind of variable maybe so that the code has to be included only once?

$(function() {
$(".link1").click(function() {
    $(".load_space").load("helloworld.txt", function() {
        //Insert contents of file wherever
        $(".block1").stop(true, true).animate({ left: -400 }, 200);
        $(".block2").stop(true, true).animate({ left: 25 }, 200);
    });
});

$(".link2").click(function() {
    $(".load_space").load("hellouniverse.txt", function() {
        //Insert contents of file wherever
        $(".block1").stop(true, true).animate({ left: -400 }, 200);
        $(".block2").stop(true, true).animate({ left: 25 }, 200);
    });
});

$(".link3").click(function() {
    $(".block2").stop(true, true).animate({ left: 450 }, 200);
    $(".block1").stop(true, true).animate({ left: 25 }, 200);
});
});
  • 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-29T06:35:23+00:00Added an answer on May 29, 2026 at 6:35 am

    There are a couple of ways.

    1. Use a map in your code.

      You could have a map in your code that tells you that link1 => helloworld.txt and link2 => hellouniverse.txt, like this:

      var map = {
          link1: "helloworld.txt",
          link2: "hellouniverse.txt"
      };
      

      Then:

      $(".link1, .link2").click(function() {
          var file = map[this.className]; // <=== Assumption here, see below
          $(".load_space").load(file, function() {
              //Insert contents of file wherever
              $(".block1").stop(true, true).animate({ left: -400 }, 200);
              $(".block2").stop(true, true).animate({ left: 25 }, 200);
          });
      });
      

      That assumes that the link1 or link2 class will be the only class on the element. If that won’t be the case, you may have to massage className a bit before using it to look up the file.

    2. Use data-* attributes.

      Add a data-file attribute to your link elements, e.g.:

      <div class="link1" data-file="helloworld.txt">...</div>
      

      Then:

      $(".link1, .link2").click(function() {
          var file = $(this).attr('data-file');
          $(".load_space").load(file, function() {
              //Insert contents of file wherever
              $(".block1").stop(true, true).animate({ left: -400 }, 200);
              $(".block2").stop(true, true).animate({ left: 25 }, 200);
          });
      });
      

      or instead of the $(".link1, .link2") selector, you could just use $("*[data-file]") or better yet, something a bit more targeted (since selecting purely on an attribute selector is a bit heavy). So perhaps $(".links[data-file]") for any element with class “links” that has a data-file attribute.

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

Sidebar

Related Questions

http://jsfiddle.net/9BCrs/ The idea is to click Slide Left and the DIV's slide left -
http://jsfiddle.net/AndyMP/nUhhf/1/ This is a simplified situation where I have a DIV that animates downwards
http://jsfiddle.net/AYPze/9/ In this example I have two similar divs with the same goal. Bind
http://jsfiddle.net/mjmitche/gq6En/62/ this fiddle allows you to drag and drop little boxes into the white
http://jsfiddle.net/mjmitche/gq6En/78/ In this fiddle, if you drag the orange box into the white box,
http://jsfiddle.net/CApW2/ I can't get it done the right way, left and right div are
http://jsfiddle.net/waitinforatrain/sEX3n/ I have two divs in a container with absolute position. Both of them
http://jsfiddle.net/B2rdD/3/ I've Created 25 buttons using HTML and I have written jquery code such
http://jsfiddle.net/nf8NM/3/ This is my first foray into Backbone and I am simply trying to
http://jsfiddle.net/nFrp7/4/ I have a layout similar to this jsfiddle I have created. I want

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.