javascript get data attribute from event

You can do either event.target.dataset.tag or event.target.getAttribute('data-tag'); either one works. Please let me know if that answers your question. Note: This is a low-level method; a more convenient .data() is also available. Here you can see that two type of get data attribute methods are :-.data(‘attribute’) method For instance, let’s say you have a list of different restaurants on a webpage. To get a data attribute we can use the following code: var element = document. You can use gtm.element.dataset.gtmAutopromo in a datalayer variable to grab the whole object, but I’m not sure how you’d break out the specific attributes. A document-wide handler tracks events, and if an event happens on an attributed element – performs the action. log (element. : The element and data- property render in html fine. There are a few options to deal with this, e.g. Custom data attributes get added to the HTML just like regular IDs and classes, and can therefore leverage all the benefits of GTM’s built-in triggers. This information might not be essential for readers, but having easy access to it would make life a lot easier for us developers. Sorry, I haven’t seen a whole JSON object stored in a data attribute before. The getAttribute() method returns the value of the attribute with the specified name, of an element. Certain elements of your page aren't actually referring to any URL, but you may want them to trigger Ajax calls. You would add this into a custom JavaScript variable in GTM, and use this Variable in place of the dataset.tilePosition data layer Variable above. getElementById ('demo'); console. Choose “Some Link Clicks” and choose the following condition: (the asterisk is to include clicks on any nested elements), You can now put this all together in Google Tag Manager to create an event tag that tracks clicks the position of each product click, like this:Track Type = Universal Analytics > EventCategory = Product TilesAction = Clicked Position {{DL – dataset.tilePosition}}Label = {{Click URL}}Trigger = Click – Product Tile. The SyntheticEvent now holds 'currentTarget', such as in event.currentTarget.getAttribute('data-username'). Getting a Data Attribute permalink. In a practical sense, data attributes also keep analytics top-of-mind (i.e. var modal = document.getElementsByClassName('modal')[0]; // get the data-modal-id attribute var modal_id = modal.dataset.modalId; console.log(modal_id); // get the data-user attribute var user = modal.dataset.user; console.log(user); Every attempt is made to convert the attribute's string value to a JavaScript value … Behavior: Counter. What is discouraged is making upyour own attributes, or repurposing existing attributes for unrelated functionality. var attribute = $(event.target.attributes['data-tag']).val(); so e.currentTarget.attributes['tag'].value works for me, javascript - props - reactjs get data attribute, http://facebook.github.io/react/docs/jsx-gotchas.html, https://reactjs.org/docs/handling-events.html#passing-arguments-to-event-handlers. If anyone is trying to use event.target in React and finding a null value, it is because a SyntheticEvent has replaced the event.target. Say you have an article and you want to store some extra information that doesn’t have any visual representation. Form event occurs when the user performs some action within the form such as submitting the form, selecting input field, etc. In the following sections we will learn about these methods in detail. For instance, here the attribute data-counter adds a behavior: “increase value on click” to buttons: Return value. Hey, Very clear explanation and uses case. How can I merge properties of two JavaScript objects dynamically? The getAttribute() accepts an argument which is the name of the attribute from which you want to return the value. However, they’re explicitly designed to contain data, so your developers can change site layout and design without altering the site elements that your tracking is based on. For example, this code sets a value for the firstName property in the user attribute. Live Demo You can also specify extra parameters through the data-params attribute.. Access the data attribute with jQuery data-. How do I loop through or enumerate a JavaScript object? Thanks for your comment! dataset. ), what if your HTML actually nests part of the product tile under the data attribute? How do I correctly clone a JavaScript object? customEmoji); // fuu // Pretty cool right! Adding data attributes to a GTM trigger lets you fire events when a user clicks on an element containing your data attribute. 3.2.2 data-url and data-params. If that doesn’t work either, you will probably need to use the “Advanced Case” script to crawl up the DOM and return the data attribute with the closest function. In GTM, navigate to Triggers > New > Click Just Links. Getting Element's Attribute Value. Add your data attribute name like this: gtm.element.dataset. Sure, the examples given in the Access Data Attributes with Javascript section don’t require click events. If so, it may help to do a link-click trigger instead of all clicks. What if you also needed to store the restaurant idto see whi… The target property gets the element on which the event originally occurred, opposed to the currentTarget property, which always refers to the element whose event listener triggered the event. Skip to main content. This is the rule, and we have to … In case the attribute does not exist, the getAttribute() returns null. So data-user-name becomes userName. This can be useful to trigger an action on check-boxes for instance: As of React v16.1.1 (2017), here is the official solution: https://reactjs.org/docs/handling-events.html#passing-arguments-to-event-handlers. Check out my API Connector Add-on to easily connect and pull data from thousands of platforms (e.g. {attribute name in camelCase}. Using the above example, if we wanted to select all the HTML elements with a data-tile-position data attribute, we could using the following JS: If we want to select HTML elements with a specific attribute value, it would be like this: Returning the values associated with data attribute requires accessing the element’s dataset property. The target event property returns the element that triggered the event. 2) get from javascript with e.target.attributes.getNamedItem (" data-mydatafield ").value If anyone is trying to use event.target in React and finding a null value, it is because a SyntheticEvent has replaced the event.target. var Socket = new WebSocket(url, [protocal] ); Key is the rest of the part after removing data-. Any attribute on any element whose attribute name starts with data- is a data attribute. 4) Get selected