0

This is my a href and I want to pass this value on a JavaScript. How do I alert it?

<a href="javascript:void();" class="right-bar-toggle" data="<?php echo "1"; ?>" >
  <i class="zmdi zmdi-notifications-none"></i>
</a>

My javascript:

$(document).ready(function () {
  var a = $(this).attr("data");
  alert(a);
});
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Jay
  • 37
  • 1
  • 6
  • `javascript:void();` is a totally useless combination of no-ops and syntax errors. And you [shouldn't use `javascript:` urls at all](http://stackoverflow.com/a/134957/1048572) (and [not even `onclick` attributes](http://stackoverflow.com/q/1070760/1048572)) – Bergi May 08 '17 at 03:59

2 Answers2

0

You need to name the data attr.

<a href="javascript:void();" class="right-bar-toggle" data-test="<?php echo "1"; ?>" >

and

$("a").data("test");
Rafael Biriba
  • 173
  • 10
0

Base on your html, your script must be

$(document).ready(function () {
  var a = $("a").attr("data");
  alert(a);
});