I can’t get value from date-Object

Asked

Viewed 58 times

3

There is the following problem I have several inputs listed with the same css class, but I need to take a data-attribute from each one by clicking on the respective input to get its attribute value:

For example

<input type="button" data-object="{"nome": "Fulano"}" class="btn btn-primary"/>
<input type="button" data-object="{"nome": "Maria"}" class="btn btn-primary"/>
<input type="button" data-object="{"nome": "José"}" class="btn btn-primary"/>
  • 2

    Do you really need to store a complete object in html? I believe it is more advantageous seed to reference it.

1 answer

4


First of all review the HTML, see the "hierarchy" of double quotes, you open and close them in the wrong way... Then try the following:

$(document).ready(function() {
   $(".btn-primary").on('click', function(){
      alert(JSON.parse($(this).attr('data-object')).nome);
   });
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<input type="button" data-object='{"nome": "Fulano"}' class="btn btn-primary"/>
<input type="button" data-object='{"nome": "Maria"}' class="btn btn-primary"/>
<input type="button" data-object='{"nome": "José"}' class="btn btn-primary"/>

  • Vlw guy worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.