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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:17:19+00:00 2026-06-15T03:17:19+00:00

I am currently teaching myself jQuery (started yesterday) and I’m stuck already.. I have

  • 0

I am currently teaching myself jQuery (started yesterday) and I’m stuck already.. I have this to create small dropdowns that will work relative to each other.

$(document).ready(function(){
  var selTxt = 0;
  $(".button").click(function(){
        alert($(this).prev(".button").attr("id"));
        $(".text").slideUp(150);
        $(this).next(".text").slideToggle(150,function(){
            selTxt = $(this).prev(".button").attr("id");
        });

  });
});

Those of you that know jQuery will realise it simply expands a textbox when clicking a button and hides all others that are expanded.

Question:

Take a look and see where the alert() function is called. On that line I wish to run an if statement.. Something like this:

$(".button").click(function(){
    if($(this).attr("id") == selTxt){
        $(".text").slideUp(150);
        $(this).next(".text").slideToggle(150,function(){
            selTxt = $(this).prev(".button").attr("id");
        });
    }
});

Hence I only want it to run if selTxt doesn’t equal the id of the element that has been clicked. So the $(this) in the if statement (I HOPED) would refer to the .button being clicked, but guess what: it doesn’t! Could anybody inform me why not? I thought because I referenced the .button with $(this) lower down that I would be able to use it there as well but evidently not.

Good old JSFiddle here.

Thanks in advance.

  • 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-06-15T03:17:20+00:00Added an answer on June 15, 2026 at 3:17 am

    As @Hawiak pointed, the problem is that your’re testing .attr("id") of all div.button but your HTML has an id attribute assigned only for the first div. But at all, I don’t believe checking by the id is the best way to do it. Here’s my suggestion:

    $(function() {
      $(".button").click(function() {
          var text = $(this).next(".text");
    
          if (!text.hasClass("open")) {
            $(".text").slideUp(150).removeClass("open");
            text.stop(true, true).slideToggle(150).addClass("open");
          }
      });
    
      $(".hideAll").click(function() {
        $(".text").slideUp(150).removeClass("open");
      });
    });​
    

    And the updated Fiddle here.

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

Sidebar

Related Questions

Hey all, I'm currently teaching myself Objective C and I'm kind of stuck. I'm
I'm teaching myself javascript/jquery and decided as a side project to create a drag
Teaching myself some c# and my problem is that I have a class which
my level of knowledge: i have just started teaching myself PHP and Dreamweaver after
I've been teaching myself functional programming, and I'm currently writing different higher order functions
I'm currently teaching myself objective C. I've gone through the tutorials, but I find
I am newbie in java but I think I have done well teaching myself
I am currently teaching myself about various data strutures and am a little frustrated
I'm interested in teaching myself different data structures, something I currently know very little
I'm teaching myself VB.Net. Here is a problem I have recently come across. Say

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.