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

  • Home
  • SEARCH
  • 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 6638839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:29:05+00:00 2026-05-25T23:29:05+00:00

I wrote this simple code to understand why if I click the click me

  • 0

I wrote this simple code to understand why if I click the “click me” text for the second time JQuery does not display the “second” text.

<div id="clickme">Click me</div>
<div id="sometext">This is text ZERO</div>

<script type="text/javascript"> 
    $(document).ready(function($) {

        $("#clickme").click(function(){ 
            if (a == 1) {
                $('#sometext').replaceWith('<div id="sometext">This is text TWO</div>');
            }
            else {
                $('#sometext').replaceWith('<div id="sometext">This is text ONE</div>');
                var a = 1;
            }
        });

    });
</script>

I expected the following behaviour:

  1. Page is loaded and the text ‘This is text ZERO’ is displayed
  2. I click the link the first time and JQuery displays ‘This is text ONE’
  3. I click the link a second time and jQuery displays ‘This is text TWO’

Instead point #3 is not processed or at least the text is not displayed. Why?

  • 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-25T23:29:05+00:00Added an answer on May 25, 2026 at 11:29 pm

    It is a scope issue as mentioned above, the variable ‘a’ must be defined in the scope of the page, not the click event.

    $(document).ready(function($) {
        var a = 0;
    
        $("#clickme").click(function() {
            if (a == 1) {
                $('#sometext').replaceWith('<div id="sometext">This is text TWO</div>');
                a = 0;
            }
            else {
                $('#sometext').replaceWith('<div id="sometext">This is text ONE</div>');
                a = 1;
            }
        });
    });
    

    http://jsfiddle.net/d5xSH/

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

Sidebar

Related Questions

I've wrote this simple piece of code. And I have a slight problem with
I wrote this simple test code, by adapting a piece from a book, to
I'm using Visual Studio 2008 for C#. I can't understand why this simple code
Hi I've wrote simple Safari extension to display QR code with url to currently
I wrote a simple program to understand how objective-c works. This program is the
I am trying to write a simple tool using Shoes. This will indent code
So I wrote this simple console app to aid in my question asking. What
This is a very simple bash script I wrote: #!/bin/bash ITEM_LIST=items.txt LOG_FILE=log.log TOTAL_ITEMS=$(wc -l
I wrote this snippet of code and I assume len is tail-recursive, but a
I did a simple test to understand how transactions work in .net Sample code

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.