Choose all of the IDs from various sections using Jquery

I am facing an issue with having 2 identical IDs inside 2 separate sections. When the button is clicked, I want it to select the ID within this section and apply a style, then also select the ID in the other section and apply that same style.

The code snippet provided below only works for the first section. Can someone please help me out?

$( document ).ready(function() {
    $('#clickMe').click(function() {
      $('#square').css('background-color', 'red');
    })
});
.block {width: 100%; padding-bottom: 40px;}
#square {width: 80px; height: 30px; border: 1px solid gray}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="block">
  This is block 1
  <button id="clickMe">Click Me</button>
  <div id="square"></div>
</div>
<div class="block">
  This is block 2
  <div id="square"></div>
</div>

Answer №1

It is important to ensure that each ID is unique on a page to avoid conflicts. If the same ID is used multiple times, the DOM will only recognize and apply attributes to the first instance it finds.

$( document ).ready(function() {
    $('#clickMe').click(function() {
      $('.square').css('background-color', 'red');
    })
});
.block {width: 100%; padding-bottom: 40px;}
.square {width: 80px; height: 30px; border: 1px solid gray}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="block">
  This is block 1
  <button id="clickMe">Click Me</button>
  <div class="square"></div>
</div>
<div class="block">
  This is block 2
  <div class="square"></div>
</div>

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

Utilize different versions of jQuery along with various plugins

I have integrated the AdminLTE Template into my project, which requires jQuery Version 3.X. Additionally, I am using the Flotchart jQuery library, which specifically needs jQuery Version 1.11.3. To handle this conflict, I have implemented the $.noConflic ...

Troubleshooting problem in AngularJS involving ng-repeat and storing user input

I am currently working on developing a system similar to Facebook's post/like/comment feature. I have successfully implemented the posts and likes functionality, but I am facing some challenges with comments. The issue lies in the controller code and ...

Loading content from another webpage within a webpage using Ajax

I successfully implemented an Ajax chat in a PHP page and it was working perfectly. However, when I tried to integrate the chat into another page using a pop-up div (via CSS with z-index), I encountered some issues. Here is the code snippet: function sho ...

The webpage content is failing to display once the HTML document has finished loading

My goal is to load an advertisement after the page has loaded using jQuery's html() method. However, I am facing an issue where the $(document).ready(function(){ function is causing a redirect, displaying only the advertisement without any other data. ...

Tips for choosing an attribute within a list with a CSS selector and Selenium

Is it possible to use a css selector to target an element within a <li> using Selenium? Below is the code snippet in question: <div id= "popup"> <ul> <li> <div id="item" option= "1" ....> </div> < ...

I'm experiencing trouble with Shopify as it appears to be failing to execute

I have been attempting to incorporate a tracking pixel into my project. Thus far, I have tested the code in various environments, both with and without wrapping it in a function and deploying it using window.onload. If you would like to see the implementa ...

Unusual behavior of diagonal viewBox in SVG

Trying to understand how the viewBox attribute works in SVG, but the code below is confusing me. Can someone please explain if this is a bug or the expected behavior of SVG? I can't seem to figure out what I'm doing wrong... <svg class="s ...

What is the best way to locate an element with the class name being an email address using jQuery?

Is it possible to locate an element with an email address as the class name, considering that the email address varies? $(document).ready(function(){ //not working getting error var email="<a href="/cdn-cgi/l/email-protection" class="__cf_email__ ...

Retrieve the second term value with jQuery UI Autocomplete

Hey @ManselUK, thanks for the helpful response! Resolved this issue by identifying values(below) However, I'm encountering a problem where selecting a value does not set the hidden element's value. Why is this happening? An error message pops ...

Transforming a form submission into an AJAX post while already in an AJAX post

I am looking to convert a form submission to an ajax post request while working within the codeigniter framework. The current JavaScript code is: $('#book-appointment-submit').click(function(event) { event.preventDefault(); var formData ...

Struggling to figure out the ::before border trim issue

As I work on designing a banner for a webpage, I have added a sliced bottom right border using the ::before pseudo class. The code snippet I used includes: &::before { content: ""; position: absolute; bottom: 0; right: 0; ...

Guidelines for implementing a seamless translation effect with CSS3

CSS: .horizon { position: absolute; top: 380px; width: 1800px; height: 50px; background: url(images/road.png) repeat-x; -webkit-animation-name: prop-600; -webkit-animation-duration: 20s; -webkit-animation-iteration-count: i ...

What is the best method for storing a model in a database?

Hello, I am currently attempting to save a model to the database. I am simply inputting the value of a title in order to save it, as my id is set to auto increment. However, I have encountered an issue where my attempts have been unsuccessful. Can someone ...

Eliminate the hovering effect from images that are hyperlinked

I am feeling incredibly desperate as I have spent hours searching the internet for a solution with no success. When it comes to text links, I have included the following CSS code: a:hover img { border-bottom: none !important; } However, this code is als ...

Refreshing a view in Laravel after a successful insertion using JQuery Ajax

I have been successfully inserting records into a table using jQuery ajax, and receiving a flash message confirming the successful insertion. However, I am now facing an issue where I do not know how to reload the table to reflect the changes after the rec ...

Dompdf is outputting the HTML Bootstrap code as col-sm-x instead of col-lg-x

Currently, I am utilizing Dompdf in my PHP project to generate PDF reports. However, I have encountered an issue where the HTML bootstrap code does not render as expected on the PDF document. Specifically, when viewing the grid on a smaller device, it coll ...

Transmitting a pair of data points to PHP using ajax POST for querying the SQL database

I am attempting to send two values from a form to another PHP file using the ajax post method. One value is the current input box value, while the other is the value being typed into a different input box which functions as a search box. When I execute the ...

Navigate to the top of the page using jQuery

Attempting to implement a simple "scroll jump to target" functionality. I've managed to set it up for all parts except the "scroll to top". The jumping works based on the tag's id, so it navigates well everywhere else, but not to the very top. He ...

Utilizing repeatable jQuery AJAX calls

I am currently working on a web application that utilizes asp.net mvc. I am using ajax requests in jQuery to display information about Clients, Staff, and Reports. I have created separate functions for each action (view, add, edit, delete) using jquery aja ...

jQuery's show/hide function exhibiting inconsistencies when checking or unchecking

I have implemented a feature on my webpage where there is a field for previous surgeries with certain elements. The goal is to display the previous surgery elements only if the "previous surgery" checkbox is checked. Below is a snippet of the code I'm ...