My desire is to select a square and activate a feature that sends a message containing the numerical value inside that square using jQuery

JQUERY

$(document).ready(function(){
    $("div").click(function(){
        var paragraphs = $("p");
        for( i=0; i<paragraphs.length; i++ ) {
        alert("Found paragraph: " + paragraphs[i].innerHTML);
    });
});

CSS

.container {
     padding:10%
}

[class*="col-"] > div {
     background:white;height:200px;width:200px;
     padding:20px;
     margin:13%;
 }

.value {
     position: absolute;
     top: 90px;
     left: 130px;
} 

HTML

<div class="container">

<div class="row">
<div class="col-md-4  col-sm-5">
<div style="background-color:pink"><h1 id="test" class="value">1</h1></div>
</div>

<div class="col-md-4 col-sm-5">
<div style="background-color:pink" ><h1 id="test" class="value">2</h1></div>
</div>

<div  class="col-md-4 col-sm-5">
<div style="background-color:yellow" ><h1 id="test" class="value">3</h1></div>
</div>

<div class="col-md-4  col-sm-5">
<div style="background-color:yellow"><h1 id="test" class="value">4</h1></div>
</div>  

<div class="col-md-4 col-sm-5">
<div style="background-color:blue"><h1 id="test" class="value">5</h1></div>
</div>

<div class="col-md-4  col-sm-5">
<div style="background-color:blue" ><h1 id="test" class="value">6</h1></div>
</div>  

Whenever a box is clicked, an alert should display containing the number inside that box.

How can this issue be resolved?

Answer ā„–1

Dimitar Popov has provided solid advice, recommending the use of

alert($(this).find(".value").html());
and incorporating it into a click handler.

It is important not to attach a handler to every single <div>. It is more efficient to target specific divs that are associated with the squares:

$(document).ready(function(){
    $('[class*="col-"] > div').click(function(){
        alert($(this).find(".value").html());
    });
});

Take a look at this Fiddle for a functional demo. I have also made some minor enhancements to your code there...

Answer ā„–2

When setting event listeners, the DOM element is obtained when the event occurs using this. Therefore, to alert the value of each h1 when the respective div is clicked, you can use the following code:

$(document).ready(function(){ 
  $("div").click(function(){
    alert($(this).find(".value").html());
  });
});

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

What is the most effective way to eliminate just the last underscore in a string by employing the

I am currently facing an issue with table cell editing. I have the following code that removes all underscores (_) from a string if it contains "test_1_". However, my requirement is to only remove the last underscore and have the string appear as "test_1". ...

Unraveling the mysteries of the Bootstrap carousel script

Hi everyone, I'm a newcomer to the world of JS and jQuery. Recently, while examining the code in carousel.js, I stumbled upon this particular line: this.cycle(true) The cycle function is structured like this: Carousel.prototype.cycle = function ...

I'm having trouble with my bootstrap dropdown and I've exhausted all of my options trying to fix it based on my current understanding

My dropdown menu is not working despite adding all necessary Bootstrap CDN and files along with the jQuery script. Even with the table being handled by JavaScript, the button does not respond when clicked repeatedly. I suspect the issue lies within the han ...

Styling an element in CSS3 based on the status of an input checkbox

Can someone explain the purpose of using '~' in this code snippet? I came across this example in a tutorial and am curious about its significance. http://css-tricks.com/the-checkbox-hack/ I understand that it alters the styling of an element bas ...

jQuery tip: prevent redundancy in your code by using fadeOut() efficiently

One common issue I encounter is: I have a potentially hidden element I need to perform certain actions on that element If the element is visible, then fade it out using fadeOut() Once the actions are completed, fade the element back in with fadeIn() The ...

What are the best practices for loading images in a slideshow?

Iā€™m working on a web application that features an image gallery with a carousel at the bottom. I recently considered a scenario where a user uploads 1000 images to the gallery. This got me thinking about the best way or technique to load all these imag ...

Ways to allocate space evenly between components of the same size in React Native

As a beginner in Javascript and React-native, I have been experimenting with the technology to assess its viability for potential use in my current workplace. However, I have encountered some challenges with the user interface. To enhance my understanding ...

trouble combining two arrays of JavaScript objects

I'm facing a challenge with an object array. I need to add a new object "WITH" a specific index, but I'm unsure of the correct approach. This is my current attempt: //ORIGINAL DATA: this.fetchedData = [{ "startMinute": 0, //Remove "annotati ...

Exploring the intricacies of Bootstrap 4 media queries through pixel widths

I am currently working on developing a web app using Bootstrap 4 as the front-end framework. The app is primarily designed for desktop use, but I have made certain views and menus adaptable to mobile devices. However, I have encountered an issue where the ...

Guide to implementing jQuery UI button to an HTML button within an ASP.NET application

My GridView contains a button named btnShowTradeScreenshot. I am looking to apply jQuery buttons to the many buttons created within the GridView. Here is the relevant code snippet from my GridView: <asp:GridView ID="grdTrades" runat="server" ...

The 'Bfrip' button is missing from the DOM

Having some issues with displaying table content using DataTables. Everything seems to be working smoothly except for the Buttons, which are not appearing as expected. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.1 ...

What impact does setting everything to position:relative have on a webpage?

I stumbled upon some CSS / HTML examples and came across this interesting snippet: *, *:before, *:after { position: relative; } It appears that implementing this rule can have a notable impact on the layout. What might be the rationale behind this ...

Customizing hover color with tailwind CSS

How can I change the color of an icon on mouseover using Tailwind CSS? I have tried to go from this https://i.sstatic.net/ObW6C.png to this https://i.sstatic.net/Tagp3.png, but it's not working. .btn { @apply agt-h-10 agt-w-10 agt-bg-zinc-100 agt- ...

Using jQuery to transform a JSON array within a JSON object into an array

I am attempting to convert the JSON data below: { "questions": [ { "question#1": "How much is 3+1", "correct answer": 1, "answers": { "ans#1": "5", "ans#2": "4", ...

Array of Javascript elements in three dimensions

Currently, I am working on creating a 3D array in JavaScript, but I am facing some uncertainty in the process. My goal is to have three arrays - Provinces, Cities, and Malls - all in a sequential order, and I am looking to build a 3D array to store all thi ...

Having trouble locating the accurate URL for the deployed server in Web Core API

I am encountering a perplexing issue when attempting to access my deployed Web API Core from AJAX, as I consistently receive a 404 error. Although I can successfully make calls to the API from C# code, the jQuery code below seems to be causing trouble: va ...

The ng-hide and ng-disable functions are ineffective

For some reason, I am struggling to get both ng-hide and ng-disable directives to work simultaneously in my table row with a button click. It seems that when I define ng-controller="FileDestroyController" alone next to one directive, the other stops functi ...

Is there a way to modify the color scheme of the hamburger icon

I'm experiencing difficulties changing the color of a hamburger icon. I've searched for the specific code responsible for the color change, but I haven't been able to locate it. Ideally, I want the color to be white, but on small screens, i ...

Adjust the alignment of two headers with varying sizes

Can someone provide guidance on aligning two headers of different sizes (e.g. h3 and h5) based on their bottom edges, making them appear like a cohesive group in a sentence format? The current code snippet I am working with is as follows: ...

jquery handling error responses

Can anyone assist me with calling a controller from jQuery? Everything seems to be working fine, but when using Ajax, an error alert is displayed. I'm not sure what is going wrong. $.ajax({ type: 'POST', ...