Switch the text within the div and alternate between showing different content in separate divs by utilizing jquery

Check out the jQuery code snippet below:


$(document).ready(function () {
    $('#prvnote').hide();
    $('#regclass').click(function () {
        $('#prvnote').toggle(400);
        $('#newdiv').toggle(400);
    });
});

Take a look at the HTML code below:

This div is only used as a menu.


<div id="regclass"><span>View Previous Note</span></div>

Now, when you click on the above div, the content in the div below will display, and the content within the span tag above will be replaced with "Post Note".


<div id="newdiv"><form>......</form></div>
<div id="prvnote"><form>......</form></div>

When div(newdiv) is shown, the span content should be "Post Note", and when div(prvnote) is shown, the span content must be "View Previous Note".

Answer №1

Give this a try

$(document).ready(function () {
           $('#prvnote,#newdiv').hide();
           $("#regclass").toggle(function () {
               $('#regclass span').text('Click to Post Note');
               $('#prvnote').toggle(400);
               $('#newdiv').toggle(400);
           }, function () {
               $('#regclass span').text('Click to View Previous Note');
               $('#prvnote').toggle(400);
               $('#newdiv').toggle(400);
           });
       });

Check out the DEMO

Answer №2

Learn HTML and jQuery

<div id="newdiv" style="display:none"><form>......</form></div>
<div id="prvnote"style="display:none"><form>......</form></div>
<div id="regclass"><span>Click here to view previous note</span></div>

Incorporating Jquery function

$('#regclass').click(function () {
alert("Hello there!");
$("#regclass span").html("").append("POST NOW");
$('#prvnote').css({"display":"block"});
$('#newdiv').css({"display":"block"});

});

Visit the provided link for reference

http://jsfiddle.net/ShekharPankaj/3htMG/

Answer №3

Check out this code snippet:

   $(document).ready(function () {
       $('#prvnote').hide();
       $("#regclass").toggle(function () {
           $('#regclass span').text('Post Note');
           $('#prvnote').toggle(400);
           $('#newdiv').toggle(400);
       }, function () {
           $('#regclass span').text('View Previous Note');
           $('#prvnote').toggle(400);
           $('#newdiv').toggle(400);
       });
   });

Sourced from a user named Sridhar R, the code aims to display one div at a time...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust time according to specified time zone using JavaScript

I am working on a project involving clocks and timezones. The user should be able to choose a timezone from a combobox, and upon selection, the main digital clock should update to display the time for that timezone. I have a text file called 'zone.txt ...

Confirm that the field is displaying correctly using Selenium

I am a newcomer to using selenium and I could really use some assistance with this task. Picture a scenario where there is a box for the user to input information; my goal is to ensure that the text field properly formats the number 1000000.00 as $1,000,00 ...

Utilizing Npm modules with a jQuery plugin

As a newcomer to Npm packages (coming from Ruby), I am attempting to load a jQuery plugin that is not available on NPM. My task involves building a Chrome extension. The code snippet below is utilized in the content.js script that is injected into the brow ...

What is the best way to handle a large number of nested AJAX GET requests?

My task involves making numerous AJAX GET requests, which must be nested because each request depends on variables from the response of the previous one. Although I was able to make multiple requests with the example below, it becomes impractical when dea ...

Tips for implementing permission-based functions in a React frontend with a Django Rest API

I am currently using Django Rest Framework in conjunction with React on the frontend. Utilizing Token Authentication has been successful for me thus far. Now, I am looking to add permission-based functions to my React frontend. Specifically, I want only th ...

NextJS: When attempting to serialize the `function` as JSON, an error occurs as it is not a JSON serializable data type

In my Firestore database, I have a collection named "mentor" that contains documents with three fields: "name", "email", and "linkedin". My goal is to fetch all the documents from this collection and pass them as props so that I can render their fields on ...

How come child elements are clipped in IE versions 9 and above when using the border-radius property with a fixed value?

When applying css border-radius:[some value] to a parent element and having a fixed positioned child element, I noticed that in Internet Explorer the child element appears to be 'clipped' into the parent. However, if I use border-radius:0 in the ...

Is there a way to continuously click on a button 99 times or until the code finishes running?

Need Assistance, Please Assist. I am encountering an issue where I have the same type of skip button with identical name and id properties for all products, but only the xpath changes. Can you provide guidance on how to efficiently click on 99 similar ski ...

Leveraging the Meteor Framework for Application Monitoring

Exploring the potential of utilizing Meteor Framework in a Monitoring Application. Scenario: A Java Application operating within a cluster produces data, which is then visualized by a Web Application (charts, etc.) Currently, this process involves using ...

Is the functionality of the jquery trigger('resize') limited to only one instance?

Here are two code snippets that I have: function adjustFooter() { var wrapper = $('#disqus_wrapper').height(); if ( wrapper > 200 ) { $(window).trigger('resize'); } }; var element = $('#disqus_wrapper&apos ...

Looking to access the layout details of an HTML element similar to what can be done in Firefox's debugger tool?

I am facing a challenge with a aspx.net grid control where I need to extract the first row's information and copy it into another table using JavaScript. To achieve this, I am attempting to calculate the width of each cell in the first row by accessin ...

To align the second child element in the middle of a parent div using a floating child element

This is the HTML structure I am working with. <div id="parent"> <div class="child"> <h1>title</h1> <p>content</p> </div> <div class="child"> <h1>title</h1> ...

Challenge with Ajax response (Access-Control-Allow-Origin)

On my webpage, there is a button. When this button is clicked, it triggers an ajax call with the following code: $ ( document ). ready(function() { $ (' #one ' ). click( function() { $ ("# one "). load ( "http://www.ggookkll.com/pinIt ...

record the values of buttons when submitted

<form method="post"> <button name="Day1" value="16"> <button name="Day2" value="32"> <input type="submit" value="Send"> </form> I am using a JavaScript function to switch the values of ...

"Implementing a function as the second parameter for passing in data with ajax requests

I'm currently working on a feature that involves displaying the names of users authorized to write news in a specific category. To achieve this, I'm utilizing the select2 plugin since there can be multiple writers contributing to a single news ar ...

Submitting a form with jQuery and refreshing a textarea

Seeking assistance with jQuery and Ajax. I am struggling to load textarea content from a file after submitting. Here is the code snippet: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="generator" content= "HTML Tidy for ...

Differences in Performance Between jQuery data() and Regular Objects

I'm currently exploring the most efficient way to bind data for an object using jQuery data versus using a traditional object setup. I'm working on establishing a model for my app, and here is the code for the object: var PersonData = function ( ...

What is the best way to verify reCAPTCHA v2 on an HTML website?

The code I'm using is giving me an error when the page loads: Cannot connect to reCAPTCHA. Please check your connection and try again. Just a reminder, I won't be incorporating any server-side code on my website. It's a simple HTML setup ...

What could be causing the rotation speed discrepancy of (1/2) in Three.js?

I'm currently simulating the earth's rotation on its axis in my project. I calculated that one complete rotation of the earth equals (2*Math.PI) radians, so to determine the earth's rotation per minute (per frame), I used the formula: (2* ...