javascript - e.target returning undefined? -


this question has answer here:

the following function returns undefined instead of id of element clicked on. why doing this?

// id of target element clicked on getid: function() {     var cell = document.getelementbyid('board');     cell.addeventlistener('click', function(e) {     return e.target.id;     }); }, 

your getid()function add listener, , don't return id...

consider snippet, it's working:

var cell = document.getelementbyid('board');  var id = cell.addeventlistener('click', function(e) {    console.log (e.target.id);  });
<body>    <div id="board">      <div id="messagearea">let's begin! enter position below. </div>      <table>        <tr>          <td id="00">00</td><td id="01">01</td><td id="02">02</td><td id="03">03</td><td id="04"04>04</td><td id="05">05</td><td id="06">06</td>        </tr>        <tr>          <td id="10">10</td><td id="11">11</td><td id="12">12</td><td id="13">13</td><td id="14">14</td><td id="15">15</td><td id="16">16</td>        </tr>        <tr>          <td id="20">20</td><td id="21">21</td><td id="22">22</td><td id="23">23</td><td id="24">24</td><td id="25">25</td><td id="26">26</td>        </tr>        <tr>          <td id="30">30</td><td id="31">31</td><td id="32">32</td><td id="33">33</td><td id="34">34</td><td id="35">35</td><td id="36">36</td>        </tr>        <tr>          <td id="40">40</td><td id="41">41</td><td id="42">42</td><td id="43">43</td><td id="44">44</td><td id="45">45</td><td id="46">46</td>        </tr>          </table>      </div>      <form>      <input type="text" id="guessinput" placeholder="a0">      <input type="button" id="firebutton" value="fire!">    </form>    </body>


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -