I was wondering what is the best way for retrieve the value of a custom data HTML attribute with Meteor from event object?
eg:
articles.html
<template name="createArticle">
<form class="new-article">
<input type="text" name="title" placeholder="New title"/>
<input type="text" name="content" placeholder="New content" />
<!-- list categ -->
<label>Category
<select id="categ-list" name="categ">
{{#each categories}}
<option value="{{name}}" data-id={{_id}}>{{name}}</option>
{{/each}}
</select>
</label>
<input type ="submit" value= "Create" class="button">
</form>
</template>
articles.js
Template.createArticle.events({
"submit .new-article": function(event){
var title = event.target.title.value;
var content = event.target.content.value;
var categName = event.target.categ.value;
var categId = event.target.categ.data('id'); // HERE
console.log("test " + categId);
Meteor.call("addArticle", title, content, categId, categName);
event.target.title.value = "";
event.target.content.value = "";
return false;
},
"click #categ-list": function(event){
console.log('click');
}
});
How can I get the data-id
attribute value in the event handler?
EDIT: Add more code
EDIT2:
console.log(event.target.categ)
output:
<select id="categ-list" name="categ">
<option value="test" data-id="p5zKaEbEiRkQjCkGg">test</option>
<option value="test1" data-id="okPY6oyeXiFR7M3jd">test1</option>
</select>
Use the target. dataset property to access data attributes from the event object in React. The dataset property provides read and write access to the custom data attributes of the element. The property returns a Map of strings which can be converted to an object.
To get a data attribute through the dataset object, get the property by the part of the attribute name after data- (note that dashes are converted to camelCase). Each property is a string and can be read and written. In the above case setting article.dataset.columns = 5 would change that attribute to "5" .
In that case, you can use the getAttribute() method. The getAttribute() method will return the string value of the specified attribute. Alternatively, you can also access attributes using the getNamedItem() method.
event. target is the element the event was targeted at, which may be inside your data-link element (like the i and span in your div ). You can use the closest method with an attribute presence selector ( [attribute-name] ) to find the data-link element: const dataElement = e.
DOM elements (HTMLElement
objects) don't have .data()
method. .data()
method belongs to jQuery objects. If you are using jQuery you should wrap the element with jQuery constructor then use the .data
method:
$(event.target.categ).data('id');
Another option is using .dataset
property 1:
event.target.categ.dataset.id;
Or .getAttribute()
method:
event.target.categ.getAttribute('data-id');
update:
You should also select the selected option before using dataset
property.
var sel = event.target.categ;
var categId = sel.options[sel.selectedIndex].getAttribute('data-id');
1. IE10 and below partially support the property. Android 2.3 cannot read data-*
properties from select
elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With