1

I have this code

 var info = $($(r).find(".BuyPriceBox")).find(".PurchaseButton").data();

r is a html page returned from an ajax GET call, which contains a button which I am trying to get the data from

which I am trying to change into the JavaScript equivalent

I can't find anything that would resemble ".data()" in JavaScript? Could anyone help me with this, and if kind enough help me translate it to JavaScript?

3
  • 2
    It is .dataset developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset Commented Apr 16, 2016 at 23:32
  • @itsgoingdown hmm, so how would I translate it into javascript wholly? Commented Apr 16, 2016 at 23:33
  • Have you tried reading the jQuery code to see how jQuery does it? JQuery is "just" JavaScript code that somebody else has written for you, so it's not a question of translating it. Commented Apr 17, 2016 at 0:12

1 Answer 1

1

Using a fake get call:

function getData(cb) {
  var html = [
    '<div><div class="BuyPriceBox">',
    '<button class="PurchaseButton" data-id="one">Click</button>',
    '</div></div>'
  ].join('');
  setTimeout(cb, 1000, html);
}

getData(function(html) {

  // create a new element and attach the html received
  var temp = document.createElement('div');
  temp.innerHTML = html;

  // Then use `querySelector` to grab the element
  // and get the dataset (an object)
  var button = temp.querySelector('.BuyPriceBox .PurchaseButton');
  var id = button.dataset.id; // one

});

DEMO

Sign up to request clarification or add additional context in comments.

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.