Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way for retrieve custom data attribute from event in Meteor?

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>
like image 438
Julien Leray Avatar asked Jun 06 '15 21:06

Julien Leray


People also ask

How do you access custom attributes from an event object in react?

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.

How can I get my data ATTR?

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" .

How do you get the data attribute value in react?

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.

What is Event target dataset?

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.


1 Answers

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.

like image 102
undefined Avatar answered Oct 12 '22 13:10

undefined