K
K
krll-k2014-11-01 11:06:35
JavaScript
krll-k, 2014-11-01 11:06:35

How to get value from html data-value attribute using jquery? How to do this without using jquery, using native browser javascript?

A bit of pre-/post-history:
Trying to do internationalization with data-values:

<p data-lang-ru="Сервис позволяет ...." data-lang-eng="The service allows ...."></p>

$('p').click(function(e){
  var eng = $(this).attr('data-lang-eng');
  var ru = $(this).attr('data-lang-ru');
  console.log(eng);
  var lang = 'eng';  // для того чтобы по клику по элементу языке менялся ru <-> eng
  $(this).html(eng);  // не работает toogle как задумалось, вставляется eng вместо того что надо
});


Achieved the initial challenge. I just used this without $() - it was my enchanting mistake, but my friend pointed it out to me

. Thanks for the answers!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cha-cha, 2014-11-01
@cha-cha

<span data-test="1"></span>

// jquery
$('span').data('test');
$('span').attr('data-test');
// vanilla js
var elem = document.querySelector('span');
elem.dataset.test;

S
Sergey, 2014-11-01
Protko @Fesor

C jQuery
Without
document.querySelector('.selector').dataset.item;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question