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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:42:54+00:00 2026-05-27T05:42:54+00:00

I have some code which performs an ajax load when a link in a

  • 0

I have some code which performs an ajax load when a link in a div element is clicked.

I now want to make the entire div clickable but not sure how to get this working with the .load. I think I know what is going wrong, but I don’t know how to fix it as I am new to jQuery – hoping someone can help.

Here is the HTML

<div class="work">
    <img class="introPic" src="images/thumb.jpg" width="250" height="99" />
    <h3>
        <img class="arrow" src="images/arrow_open.gif" alt="&gt;" />
        <a class="titlelink" href="project2.html">Project 2</a>
    </h3>
    <div class="projectIntro">
        <p>This is some intro text for project 2</p>
    </div>
    <div class="pictures"></div>
</div>

Here is the complete code block as it stands now:

$('div.work').on('click',function() {
    window.location = $(this).find("a").attr("href"); 
    return false;

    var parent = $(this).parents(".work");
    var content_holder = parent.children(".pictures");

    if (parent.hasClass("selected_work")) {
        close_other();
        return;
    }

    close_other();

    parent.addClass("selected_work");

    content_holder.load(this + " #ajaxContent", function() {

    });

    $('.selected_work img.arrow').attr("src", "images/arrow_close.gif");
});

function close_other() {
    var selected_work = $('.selected_work');

    selected_work.children('.pictures').empty();    
    $('.selected_work img.arrow').attr("src", "images/arrow_open.gif");
    selected_work.removeClass("selected_work")
}

$('div.work').click(function() {
    $('html,body').animate({scrollTop: $(this).offset().top}, 500);
});      
});  

The first problem is that when the div is clicked, it finds the href of the anchor inside but loads it in a new window as opposed to in the current window as desired. I presume this is because of the window.location – it finds the link and goes there whereas I want it to find the link and pass it to the ajax load.

The second problem is that the variables are now wrong. While the code was correct when an anchor inside .work was clicked (and the selector was $(.work a)– .work was therefore the anchor’s parent but that is no longer the case:

I don’t need a parent any more as the parent (.work) is what I am now clicking on so I guess it becomes simply $(this) – therefore logically I want something like:

    var content_holder = $(this).children(".pictures");

    if $(this).hasClass("selected_work")) {
        close_other();
        return;
    }

but that has a syntax error so it can’t be right…

Then

parent.addClass("selected_work");

should probably change to something like

$(this).addClass("selected_work");

I would really appreciate some help getting the passing of the href and the syntax right for this as I’ve been trying to get it working for 2 days now. A working example would be the icing on the cake. Many thanks!

  • 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-27T05:42:55+00:00Added an answer on May 27, 2026 at 5:42 am

    Because return false; rest of your code doesn’t execute. You can change hash with location.hash:

    var lastPart = $(this).find('a').attr('href').split('/');
    var hash = lastPart[lastPart.length - 1];
    
    • Get href attribute
    • Split it with /
    • Take the last part

    You don’t have to use .parents() because $(this) refers to .work so final code will be:

    $('div.work a').click(function(e) { e.preventDefault(); });
    $('div.work').bind('click', function() {
        var href = $(this).find('a').attr('href'),
            lastPart = href.split('/'),
            hash = lastPart[lastPart.length - 1];
    
        var $this = $(this);
        var content_holder = $this.children(".pictures");
    
        close_others($this);
        if ($this.hasClass("selected_work")) {
          return;
        }
        else {
          window.location.hash = hash;
          $this.addClass("selected_work");
        }
    
        content_holder.load(href + ' #ajaxContent');
    
        $this.find('img.arrow').attr("src", "images/arrow_close.gif");
    });
    function close_others($current) {
      $('.selected_work').not($current).removeClass("selected_work")
        .find('.pictures').empty().end()
        .find('img.arrow').prop("src", "images/arrow_open.gif");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Java code which performs bitwise operations on a BitSet. I have
I have an idea for a hobby project which performs some code analysis and
I am currently refactoring some code which performs Windows Impersonation for testability and have
I have a form on my PHP page which performs some ajax validation (that's
I have written some code which performs a Monte Carlo simulation and produces curves
I have some code which collects points (consed integers) from a loop which looks
I have some code which utilizes parameterized queries to prevent against injection, but I
I have some code which I am making available via RMI. If my program
I have some code which needs to ensure some data is in a mysql
I have some code which returns InnerXML for a XMLNode. The node can contain

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.