Link to home
Start Free TrialLog in
Avatar of Whing Dela Cruz
Whing Dela CruzFlag for Anguilla

asked on

innerHTML

Hi experts,
This code allows to put in an alert the input type="text" next to the button. But what about innerHTML? What I mean is that if not input text? I remove the input text and change to <td>55</td>. Here I want to read 55, which part of this code should i change? alert($(this).closest('tr').find('input').val()); Thank you, experts!
 

<script src="http://code.jquery.com/jquery.js"></script>
<script>$(function() {
  $('button').click(function() {
    alert($(this).closest('tr').find('input').val());
  });
});
</script>
Avatar of hielo
hielo
Flag of Wallis and Futuna image

.val() will give you the value of some form input element.
for non-form elements -- ex: <td><span>55</span></td> -- you can use either .text() or .html():
$(this).closest('tr').find('td:eq(0)').text() -- will give you "plain" text -- "55"
$(this).closest('tr').find('td:eq(0)').html() -- will give you html -- "<span>55</span>"

Open in new window

Avatar of Whing Dela Cruz

ASKER

Hello Heilo,
Sorry for not explaining well my target. This is what I'm looking for. I have to cells on a table, Cell(button) and td(itself)
    <tr>
      <td><button class="Addbutton">Try it</button></td>
      <td>123</td>
    </tr>
Now, here I want to put the 123 to the messagebox or alert upon pressing the button. The codes you gave will get the value of the button itself as I tried and tested it. Thank you!
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfectly correct, sorry for not looking the codes well!
Thank you Heilo for helping me solve the problem. More power to you and God bless!
>> Thank you Heilo
It's Hielo, and you are welcome.
Although it's already answered, here's a solution similar to what I had for your previous question, using plain JavaScript.
test2.html