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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:44:01+00:00 2026-05-31T10:44:01+00:00

I’m currenty trying to create a nice nested navigation menu. The problem I face

  • 0

I’m currenty trying to create a nice nested navigation menu. The problem I face is that I want to make some elements change in a different way each time it is clicked.

I don’t really gasp how the DOM Tree respects my provious manipulations via Jquery…for example if i set a new class for a clicked element via the attr() function I’m not able to select the newly changed element with its class atribute.

Maybe I’m totally on the wrong track and this approch is wrong or must be tought in a different way. I would be so happy to hear from more experienced people how they make consecutive manipulaions on elements.

Here is what i try to achive: 1.) Trun it blue when clicked, 2.) turn it red when clicked again.

Here is a fiddle for this: http://jsfiddle.net/YsQTL/

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                *//First time clicked--> this works*
                $("h1").click(function() {
                    $('h1').css({"color":"blue"}).attr('class','clicked').text('I should turn red if clicked again');
                });
                *//Second time clicked with added class "clicked"--> this doesn't work....why?*
                $(".clicked").click(function() {
                    $('h1').css({"color":"red"})
                    });
            });


        </script>
    </head>
    <body>
        <div id ="navigation">
            <h1>Title to be changed to blue if clicked</h1>
        </div>
    </body>
</html>
  • 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-31T10:44:02+00:00Added an answer on May 31, 2026 at 10:44 am

    The biggest problem is that you are binding the second event to elements that responds to the selector .clicked on page load, that is to say, none.

    Instead, you could do something like this:

    $("h1").click(function() {
        if ($(this).hasClass('clicked')) {
            $(this).css({"color":"red"});
        }        
        else {
            $(this).css({"color":"blue"}).addClass('clicked').text('I should turn red if clicked again');
        }
    });
    

    This will bind the event to all elements responding to the selector "h1", i.e. all your h1 tags present on the page at page-load, and inside the event handler, we check if the element has the class.

    Also, keep in mind that since you were using the selector $("h1") instead of $(this) inside your event handler, every single h1 element on the page would get updated by your code, instead of just the one you actually clicked.

    Fiddle: http://jsfiddle.net/YsQTL/3/

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.