-1

I have three dynamically generated div. Each contains different data and buttons.On each button click I want to alert the data. I have this. but it only shows the first div data for each button click.

form. this form generate dynamically

<input type="hidden" name="price" id="price" class="price" value=" 
{{$data->price}}"/>
<input type="submit" name="submit"  id="button1" class="btn btn-danger 
 btn-lg raised button1"/>
<div>


<script>
    $('.button1').on('click', function(){
    var id = $('.price').val();
    alert(id);
    });

 </script>
Rithin
  • 11
  • 1
  • 7
  • Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) We obviously can't help you without seeing the HTML this relates to. Please update your question with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button; [here's how to do one](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-do-a-runnable-example-with-stack-snippets-how-do-i-do-tha)). – T.J. Crowder Jun 11 '18 at 10:05
  • That said, I will note that you have `$('.id').val()`. The `.` is for classes. If `id` is an ID, you'd want `$("#id").val()`. More in [the spec](https://www.w3.org/TR/selectors-3/), and [jQuery's documentation](http://api.jquery.com). – T.J. Crowder Jun 11 '18 at 10:06
  • Have you tried changing $('.button1') to $(this)on('click', function(){}? – MiksMeister Jun 11 '18 at 10:08
  • Thanks. I tried with # also. But same id value for each button click. – Rithin Jun 11 '18 at 10:08
  • Possible duplicate of http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element – T.J. Crowder Jun 11 '18 at 10:09
  • @Newbee1 - Again, please update your question with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button; [here's how to do one](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-do-a-runnable-example-with-stack-snippets-how-do-i-do-tha)). – T.J. Crowder Jun 11 '18 at 10:10
  • @MiksMeister I tried but still not working. – Rithin Jun 11 '18 at 10:11
  • `I want to alert the data` What data? – Keith Jun 11 '18 at 10:13
  • @ T.J. Crowder I have updated my question. – Rithin Jun 11 '18 at 10:18
  • @Keith Data inside div – Rithin Jun 11 '18 at 10:18
  • `$(this).closest("div").find("[name=price]").val()` – Keith Jun 11 '18 at 10:35

2 Answers2

0

Try below code:

  $('button').on('click', function(){
   var id = $(this)[0].id;
   alert(id);
 });
-1

Since you told that your divs are dynamic generated, you've to bind your click event through to another static element or document. This should help

$(document).on('click', '.button1' function(){ var id = $('.id').val(); alert(id); });

Brissy
  • 134
  • 1
  • 9
  • If so, this question is a duplicate of http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element and should be closed (with a comment saying how it relates, usually). – T.J. Crowder Jun 11 '18 at 10:10