Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery accessing html5 data attribute

On click of one of the options I am trying to get the data value from the 'ul li a' and place it into button below, I've set up a fiddle example here: http://jsfiddle.net/q5j8z/4/

But cant seem to get it working

$('ul li a').click(function(e) {
    e.preventDefault();

    var value = $(this).data();

    $('.button').data('value');
});

Does anyone have any ideas please?

like image 300
Adam Avatar asked Jun 05 '13 09:06

Adam


3 Answers

You can do this:

$('ul li a').click(function (e) {
    e.preventDefault();
    var value = $(this).data('value');
    $('.button').data('value', value);
    console.log($('.button').data('value'));
});

Here, $(this).data('value') is used to get the data attribute value of the link.

and $('.button').data('value', value) is used to set the data attribute value of the button.

Using, console.log($('.button').data('value')); you can check the console the data value being set.

FIDDLE DEMO

For more info:- .data() API Documentation

like image 190
palaѕн Avatar answered Sep 26 '22 00:09

palaѕн


Use it like this:

var value = $(this).data('value');

And then:

$('.button').data('value', value);
like image 36
A. Wolff Avatar answered Sep 24 '22 00:09

A. Wolff


You are not assigning the value data to the button actually. Try this:

$('ul li a').click(function(e) {
    e.preventDefault();

    var value = $(this).data();

    // assign the value form the clicked anchor to button value data
    $('.button').data('value', value);

    console.log($('.button').data('value'));
});
like image 31
thomas Avatar answered Sep 25 '22 00:09

thomas