1

I have some jquery question : I generate with php loop a list of div. I would like when I click on 1 div, print alert with the value of the div.

First my html (result of php loop) code :

<div id="unlink_1" class="unlink_table" value="1" />
<img src="images/picto-table-unlink.png" />
</div>

<div id="unlink_2" class="unlink_table" value="2" />
<img src="images/picto-table-unlink.png" />
</div>

<div id="unlink_3" class="unlink_table" value="3" />
<img src="images/picto-table-unlink.png" />
</div>

My jquery code :

$(".unlink_table").click(function(){
    var test = $(".unlink_table").val();
    alert(test);
});

For that moment it doesn't work (alert empty)

Thank you !

4
  • 1
    use $(this).html(); Commented Nov 5, 2014 at 9:35
  • does value attribute for div works? Commented Nov 5, 2014 at 9:36
  • Try with $(".unlink_table").text(); Commented Nov 5, 2014 at 9:37
  • in layman's language..jQuery doesn't know that for $(".unlink_table").val(); which unlink_X divs value needs to be picked...so you need this to point it out! :) Commented Nov 5, 2014 at 9:38

8 Answers 8

4

Try this : Inside click event you have to use $(this) to refer to clicked element. You were calling $(".unlink_table").val(); which will try to call val() for all element with class="unlink_table". Also .val() will not work for div, you have to use .attr('value').

$(".unlink_table").click(function(){
    var test = $(this).attr('value');
    alert(test);
});

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

div has no val(); should use html(); instead.
Thats ok now. Thanks for the fix.
+1 One of the few people who actually read the question before answering.
1

To select the element on which you currently clicked, you should use $(this) function in your event handler.

No value attribute is available for div tag. use data attributes instead.

HTML:

<div id="unlink_3" class="unlink_table" data-value="3" />
    <img src="images/picto-table-unlink.png" />
</div>

jQuery

$(".unlink_table").click(function(){
    var test = $(this).attr('data-value');
    alert(test);
});

OR you can ad a hidden input in each div.

HTML:

<div id="unlink_3" class="unlink_table" />
    <input type="hidden" value=3" />
    <img src="images/picto-table-unlink.png" />
</div>

jQuery

$(".unlink_table").click(function(){
    var test = $(this).find('input').val();
    alert(test);
});

Comments

0

There is no value attribute for div

$(".unlink_table").click(function(){
    //var test = $(this).html();
    //or
    var test= $(this).attr("value");

    alert(test);
});

1 Comment

You can use attr('value') as value is defined in html for div
0

No value method available for block element (DIVS) use HTML

 $(".unlink_table").click(function(e){
    var abc= $(this).html();
    alert(abc);
  });

Comments

0

you can also use somethig like this.

$('.unlink_table').on('click',function(){ 
var test = $(this).html();
alert(test);
});

this is very helpful when your using ajax calls because it keep DOM alive.

Comments

0

try with $(this).attr() you have added attribute value to div so get with attr() and $(this) refer to current div

$(".unlink_table").click(function(){
   var val = $(this).attr('value');
   alert(val);
});

Comments

0

you can try this one :

$("div[id^='unlink_']").click(function(){
    alert($(this).attr("id"));
});

Comments

0
      $(".unlink_table").click(function(){
         var test = $(".unlink_table").attr('value');
         alert(test);
      });

Hope the above code will work for you. As DIV does not have the "VALUE" attribute in it you can't print it using val() method. I would like to request you to use data-val instead of value and use the following code :

        $(".unlink_table").click(function(){
         var test = $(".unlink_table").data('val');
         alert(test);
      });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.