1

How to get the value in one metadata, based on event click in another metadata?

<button type="button"
        class="btn btn btn-danger"
        data-action="voltar-tela-pedidos"
        data-scroll-to="pedido-00C407948 ">
</button>

jQuery(document).on("click", '[data-action="voltar-tela-pedidos"]', function (e) {
  // i want to know the value of data-scroll-to
});
6
  • you have $(this) inside the jquery function, you can find all data property using $(this) Commented Aug 23, 2017 at 16:36
  • jQuery(this) is what you're looking for. var myValue = jQuery(this).data('scroll-to'); Commented Aug 23, 2017 at 16:37
  • When i use this,i recive the error : $(...).data is not a function Commented Aug 23, 2017 at 16:38
  • 1
    Try: jQuery(this).data('scroll-to'); Commented Aug 23, 2017 at 16:39
  • @RafaelUmbelino I have added to my answer an explanation as to why $() doesn't work and jQuery() does. Commented Aug 23, 2017 at 16:45

3 Answers 3

1
jQuery(document).on("click", '[data-action="voltar-tela-pedidos"]', function(e) {
    var myScrollTo = jQuery(this).data('scroll-to');
    // or
    var myScrollTo = jQuery(this).attr('data-scroll-to');
});

Check demo fiddle here.

jQuery(document).on("click", '[data-action="voltar-tela-pedidos"]', function(e) {
  var myScrollTo1 = jQuery(this).data('scroll-to');
  // or
  var myScrollTo2 = jQuery(this).attr('data-scroll-to');
  console.log(myScrollTo1, myScrollTo2)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="btn btn btn-danger" data-action="voltar-tela-pedidos" data-scroll-to="pedido-00C407948 ">CLICK ME</button>


$(...).data is not a function

When I use this, I receive the error: $(...).data is not a function

When you add jQuery to your page, it "registers itself" in the global variables $ and jQuery.

But those vars can be overridden.

Probably, in your web page context, $ is not jQuery but some other lib*. In this case, always use jQuery(...) and not $(...).

* Prototype also uses the global variable $, which could be what is overriding jQuery. I have also seen some RichFaces/JSF based applications use $ global variable.


How to find out if $ is jQuery?

Simply type in your console:

console.log($)
console.log($.fn.jquery)

If you $ is jQuery, it should output something like (the second line is the jQuery version loaded):

// this is what stackoverflow.com prints for those commands right now. try it out.
ƒ (a,b){return new n.fn.init(a,b)}
1.12.4

Older jQuery versions change a little, but the second command (console.log($.fn.jquery)) has always printed the version.

If you get an error while executing those commands, your $ is probably some other lib. In that case, confirm that jQuery at least loaded by checking your jQuery variable:

console.log(jQuery)
console.log(jQuery.fn.jquery)

The output should be similar to the example shown above.

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

Comments

0
jQuery(document).on("click", '[data-action="voltar-tela-pedidos"]', function(e) {
    console.log($(this).data("scrollTo"));
});

Comments

0
$('[data-action="voltar-tela-pedidos"]').click(function() {
  var scrollTo = $(this).data("scrollTo");
});

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

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.