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.
jQuery(this)is what you're looking for.var myValue = jQuery(this).data('scroll-to');jQuery(this).data('scroll-to');$()doesn't work andjQuery()does.