Eric
Eric

Reputation: 11

Keypress and values

I created a table and fill it with random numbers . Now I want to find them, at the same time use the keypress. If a match is found , select a cell in a different color. How to convert a understandable value eventObject.which

 function keyPress()
    {
        $('#search').keypress(function (eventObject) {
            $('td').each(function(index, element)
            {
                $(element).val() = eventObject.which

            });
        });

    }

Upvotes: 0

Views: 33

Answers (2)

Wesley Smith
Wesley Smith

Reputation: 19571

Since your elments are tds, you'll need to use html() not val() to get or set their contents. see below:

$('#search').keyup(function (e) {
       //var pressed = String.fromCharCode(e.keyCode);
       var search = $(this).val();
      $('td').removeClass('found');
       $('td').each(function(index, element){
                if($(element).html() == search){
                  $(element).addClass('found');
                }

       });
});
.found{
  background-color:green;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="search" value="" />
<br>
<br>
<table width="600" border="1">
  <tbody>
    <tr>
      <th scope="col">&nbsp;</th>
      <th scope="col">&nbsp;</th>
    </tr>
    <tr>
      <td>123</td>
      <td>234</td>
    </tr>
    <tr>
      <td>345</td>
      <td>456</td>
    </tr>
  </tbody>
</table>

Upvotes: 1

Sterling Archer
Sterling Archer

Reputation: 22425

the jQuery val() method is a getter and setter method, you don't assign to it, you pass in parameters.

$(element).val(eventObject.which);

Upvotes: 0

Related Questions