0

I would like to take the value of an attribute, process it and then replace a PHP variable with the value we receive at the end.

The data attribute is data-id, all I need assistance with is getting the value of data-id into Javascript on a click event, the rest I can do.

An example:

Person clicks an tag with the data-id set as, say, 7. I'd then need to extract that 7 from the HTML code and into a Javascript variable so I can work with said value.

I hope this is clear enough

1
  • try this $(selector).data('id') Commented Oct 11, 2014 at 17:40

3 Answers 3

2

This will do the trick.

$(selector).click(function(){
    var dataAttrId = $(this).attr('data-id');
    // Your code goes here...
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

$('#someelement').click(function(){
    $(this).data('id');
});

Comments

0

Use this

jQuery('your id or class or element').click(function(){
    var mainData= jQuery('your id or class or element').attr('data-id');
alert(mainData);
});

Comments

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.