javascriptretrieve the second class from the element

HTML Code :

<span class="button" onclick="javascript:buttonClicked();">Log In</span>       
<div class="modal-bg" style="display: none;">
    <div class="modal">
        <span>Log In<a href="#close" class="close">&#215;</a></span>
        <form>
            <input id="username" name="username" type="textbox" placeholder="Username" required>
            <input id="password" name="password" type="password" placeholder="Password" required>
            <a id="forgot-link" href="#">Forgot password?</a>
            <button name="submit" id="submit" type="submit">Log in</button>
        </form>
    </div>
</div>

For the Log In button. For the Sign Up:

<span class="button" onclick="javascript:buttonClicked();">Sign Up</span>
<div class="modal-bg" style="display: none;">
<div class="modal">
<span>Sign Up<a href="#close" class="close">&#215;</a></span>
<form>
    <input id="FirstName" name="FistName" type="textbox" placeholder="First Name" required>
    <input id="LastName" name="LastName" type="textbox" placeholder="Last Name" required>
    <input id="email" name="email" type="email" placeholder="Email" required>
    <input id="username" name="username" type="textbox" placeholder="Username" required>
    <input id="password" name="password" type="password" placeholder="Password" required>
    <br><input style="margin: 0 -102px 0;" name="sex" type="radio" value="Male" checked="checked">Male
    <input style="margin: 0 -105px 0;" name="sex" type="radio" value="Female">Female<br>
     &nbsp;&nbsp;&nbsp;Date of Birth<br>
    <select name="month" onChange="changeDate(this.options[selectedIndex].value);">
    <option value="na">Month</option>
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
    </select>
    <select name="day" id="day">
    <option value="na">Day</option>
    </select>
    <select name="year" id="year">
    <option value="na">Year</option>
    </select>
    <input id="Address" name="Address" type="textbox" placeholder="Street address">
    <button name="submit" id="submit" type="submit">Sign up</button>
</form>
</div>
</div>

**JavaScript Code:

<script>
      $('.button').click(function(){
      $('.modal').css('display','block');
      $('.modal-bg').fadeIn();
});    
</script>

<script>
          $('.close').click(function(){
          $('.modal-bg').fadeOut();     
          $('.modal').fadeOut();
      return false;
    });    
</script>

<script>function buttonClicked()
{
$('.modal').show();
}
</script>

When the Log In or Sign Up buttons are clicked, a pop-up window will appear. The issue arises when clicking either button as the Sign Up modal appears since it is coded secondly. To fix this problem, you can differentiate between the two classes by modifying the JavaScript code. You may need to adjust the identifiers or add specific classes to distinguish between the first and second modals.

Answer №1

Make sure to utilize the .next() method.

$('.button').click(function(){
      $(this).next('.modal-bg').css('display','block');
      $(this).next('.modal-bg').find('.modal').fadeIn();
});  

View Working Demo

Alternatively, you can use this direct approach:

$('.button').click(function(){
      $(this).next('.modal-bg').fadeIn(2000);
});   

View Working Demo

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

Dynamic parallax or stacked vertical text animation

I am attempting to replicate the text effect found on . The text is centered both vertically and horizontally within each div, but as you scroll down, the top position shifts creating a parallax effect. As you continue scrolling, the text transitions to th ...

When using node.js express, the req.body object does not yield any results

My web server setup with express appears to be functioning correctly, however, I am encountering an issue where the req.body returns nothing. Despite not receiving any errors, the console.log(req.body) doesn't show anything in the console output. Stra ...

Sending Twilio SMS Data from Node to React can be achieved by passing the post data

Currently utilizing Twilio for receiving SMS messages on my server, I am seeking a way to display the returned data in React. As Twilio sends data only server-side via a POST request when a text is sent from my phone, how can I access this POST data in m ...

Tips for making content vanish or remain concealed behind a see-through header during page scrolling

I made a JavaScript fiddle http://jsfiddle.net/claireC/8SUmn/ showcasing a fixed transparent header. As I scroll, the text scrolls up behind it. How can I make the text disappear or be hidden behind the transparent div when scrolling? Edit: I should also ...

Generating Dynamic Widgets Within the Document Object Model

Currently, I am working with jQuery Mobile 1 alpha 1. In order to create a text input field using jQuery Mobile, you need to include the following HTML code: <div data-role='fieldcontain'> <label for='name'>Text Input:</ ...

Load the file's contents into an array within a Vue component

https://i.sstatic.net/gxcYi.png My media.txt file contains the following URLs: "https://www.dropbox.com/s/******/687.jpg?dl=0", "https://www.dropbox.com/s/******/0688.jpg?dl=0 Incorporating a Vue carousel component: <template> <section> ...

AngularJS radio button slider with ng-model and ng-checked functionality

I'm facing an issue where my ng-model is not getting updated when I cycle through radio button images using arrows instead of clicking on the image. How can I resolve this? HTML <div ng-repeat="contact in contacts" ng-show="showContactID == ...

When the getImageData event is triggered upon loading

Hello, I have a script that identifies transparent and non-transparent pixels. Currently, the result is displayed from a 100px by 100px rectangle on mouseover: var data = ctx.getImageData(100,100, canvas.width, canvas.height).data; During mouseover, it s ...

Mastering Dropdown Navigation and Value Setting in Angular

I am facing a challenge with two components named ComponentA and ComponentB. In ComponentB, there is a link that, when clicked, should navigate to ComponentA and send specific data to it. This data needs to be displayed in the dropdown options of Component ...

Error: JavaScript encountered an unterminated string literal issue

Whenever I try to add the following two lines in the section of my application's homepage, my browser throws a JavaScript error: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script>window. ...

Steps to modify the look of text upon button click using jQuery and utilizing the information from a table row

Currently, I am working on a jquery code. My issue revolves around utilizing strike-through in a row that contains "101", with each number being stored in a table. For example: 1 0 1 1 1 0 1 All this data is within a table structure. My goal now i ...

What is the best way to transform all elements on a webpage into a dynamic scrolling marquee?

Is there a way to make every div in a web application scroll like a marquee? Perhaps by using a specific CSS class or other method? The application is built with Angular 4 and Bootstrap. ...

Error message in React/ Ruby on Rails: Encountered Uncaught TypeError when trying to access properties of undefined (specifically, 'id') while passing down a mapped prop

I have been grappling with this bug for the past few days and haven't found a solid solution yet. My objective is to route to an individual component, [http://localhost:4000/hris/employees/1], and display that specific 'employee' card on a ...

Trouble with CSS sprites displaying text alongside images

My current CSS code is set up to display images using CSS image sprites as background images. The images are displayed based on the file extension, however, the href/link text is not appearing in line and is splitting into two lines. a[href$='.pdf& ...

Can dynamic loading JavaScript be debugged using a debugger such as WebKit, FireBug, or the Developer Tool in IE8?

After asking a question on Stack Overflow, I have successfully written JavaScript functions for loading partial views dynamically. However, debugging this dynamic loading script has been a challenge for me due to all loaded JavaScript being evaluated by th ...

The Jquery Countdown plugin fails to initialize

Currently struggling with getting the jquery Countdown plugin to work for a 12-day countdown. Despite all efforts, the countdown does not seem to start and I am at a loss on how to troubleshoot this issue. After searching online extensively, I have yet to ...

Issues with anchor tag click event in Firefox browser not functioning as expected

I have encountered an issue while trying to create an anchor tag in a TypeScript file. When clicking on this button, the anchor tag should be created. This functionality is working correctly in Chrome and IE, however, it seems to be not working in Firefo ...

"Enhance Your Website with Drag-and-Drop Cart Functionality using HTML

Seeking assistance from anyone who has the knowledge. I've searched through similar questions on this platform but haven't been able to resolve my issue. Currently, I'm working on developing a basic shopping cart using HTML5 (Drag and Drop ...

What is the best way to keep my layout component fixed in the Next13 app directory?

I am struggling to develop a custom layout component that can retrieve its own data. Despite adding 'cache: 'force-cache'' to the fetch function, the updated content from my CMS is still being loaded every time I refresh the page. Below ...

Tips for inserting a row component into a table using Angular 7

I am currently using the latest version of Angular (7.2.0). I have created a custom tr component as follows: import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-table-row', templateUrl: './table- ...