Search code examples
javascriptpolymerpolymer-2.x

Get value of button in polymer


I'm trying to get the set value from a button to further procces this in my function.

<template is="dom-if" if="[[Student.Absence]]">
        <td><button class="defaultbutton" value="[[Student.studentnum]]"></button></td>
        <td><button class="absentbutton" value="[[Student.studentnum]]" on-click="absenceHandler(this.value)"></button></td>
     </template>




 absenceHandler(studID){
    let DummyStudents = this.get(['DummyStudents']);
    console.log(studID);
    alert(DummyStudents[1]);
  }

I expect the value to be inserted into my function but instead i get this:

listener method absenceHandler(this.value) not defined handler @ template-stamp.html:98 (anonymous) @ templatize.html:160


Solution

  • There is no need of sending the value to the method explicitly. You can make it work as follows.

     <td><button class="absentbutton" value="[[Student.studentnum]]" on-click="absenceHandler"></button></td>
    
    
    absenceHandler(e){
    let studID = e.currentTarget.value;
        let DummyStudents = this.get(['DummyStudents']);
        console.log(studID);
        alert(DummyStudents[1]);
      }