Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: get value from clicked div

Tags:

jquery

<div>'.$i.'</div>

$i is auto generated by loop - which could lead to:

<div>'.$i.'</div>
<div>'.$i.'</div>
<div>'.$i.'</div>

etc. where each $i is different.

How do I get value of particular $i (using jQuery), when div is clicked.

In standard JS I would use onClick($i). In jQuery I just do not know, how to pick that val.

like image 356
Jeffz Avatar asked Aug 31 '10 22:08

Jeffz


People also ask

How to get value on click in jQuery?

Answer: Use the jQuery val() Method You can simply use the jQuery val() method to get the value in an input text box. Try out the following example by entering something in the text input box and then click the "Show Value" button, it will display the result in an alert dialog box.

How do you find the value of clicked elements?

To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.

How to get class of clicked element?

To find the class of clicked element, we use this. className property. The className property is used to set or return the value of an element's class attribute. Using this property, the user can change the class of an element to the desired class.

How to get ID in jQuery on click?

You can just utilize the jQuery attr() strategy to get or set the ID property estimation of a component. The accompanying model will show the ID of the DIV component in an alarm box on a button click.


2 Answers

If you don't have any other way to identify the <div> elements, this would place a handler on every <div> on the page.

$('div').click(function() {
    var text = $(this).text();
    // do something with the text
});

The .text() method will return the text content for that <div>, (as well as any nested elements).

If you only wanted the click event on certain <div> elements, the best is to add a class, and select the correct ones based on that.

$('div.myClass').click(function() {
    var text = $(this).text();
    // do something with the text
});

HTML

<div class="myClass">'.$i.'</div>
<div class="myClass">'.$i.'</div>
<div class="myClass">'.$i.'</div>

<div>some other div</div>

If the <div> elements are all within the same ancestor element, you could use .delegate() instead, which will place one handler on the ancestor to handle all divs inside.

$('#parentID').delegate('div.myClass', 'click', function() {
    var text = $(this).text();
    // do something with the text
});

HTML

<div id="parentID">
    <div class="myClass">'.$i.'</div>
    <div class="myClass">'.$i.'</div>
    <div class="myClass">'.$i.'</div>
</div>

(Requires jQuery 1.4 or later)

like image 199
user113716 Avatar answered Sep 21 '22 02:09

user113716


$('div').click(function(event){
  alert($(this).text());
});

A more efficient solution (since it seems you have a lot of <div>s would be to add a live event to the wrapping element of those, like this:

$('#container').live('click', function(event){
   if(event.target.tagName == "DIV") alert($(event.target).text());
});
like image 31
Pablo Fernandez Avatar answered Sep 22 '22 02:09

Pablo Fernandez