0

I have a table with 2 rows having 2 columns each. I want to change the innerHTML of the td when i click a td. That is when i

<table border="1">
<tr id="r1">
<td id="b1_1" onclick="b(b1_1)">0</td>
<td id="b1_2" onclick="b(b1_2)">0</td>
</tr>
<tr id="r2">
<td id="b2_1" onclick="b(b2_1)">0</td>
<td id="b2_2" onclick="b(b2_2)">0</td>
</tr>
</table>


function _(x){
return document.getElementById(x);
}

function b(bid){
_x(bid).innerHTML = "5";
}
1
  • 3
    There is no _x function, as far as I can see.. Commented Nov 15, 2013 at 13:19

3 Answers 3

2

b1_1 is an identifier (for an undefined variable). You want a string literal. 'b1_1'

Ditto for all your other arguments.

Additionally, you are trying to call a function called _x but have defined a function called _.


It would be neater to use a single event listener and not use ids at all though.

function b(event){
    event.target.innerHTML = 5;
}

document.querySelector('table').addEventListener('click', b);
Sign up to request clarification or add additional context in comments.

Comments

1

Your function is called incorrectly (_x) - You have declared it as simply _. So your b function should look like this:

function b(bid){
  _(bid).innerHTML = "5";
}

The parameters passed in the onclick handler would also need to be strings, hence be enclosed in quotes, i.e: onclick="b('b1_1')"


Alternatively, you could just use this in those onclick handlers:

<td id="b1_1" onclick="b(this)">0</td>

And your function would become:

function b(bid){
  bid.innerHTML = "5";
}

Comments

0

Alternative

Remove your onclick on your HTML, and, using jQuery :

$('tr td').click(function(){
    var previousVal = parseInt($(this).html());
    $(this).html(previousVal + 5);
});

It will add 5 each time you click on a div (I think this might be what you want, as you're talking about bids).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.