1

I need to automatically populate certain cells of a table according to the cell name (that as to be a number) but when you call the cell by it's name it gets interpreted as it's index number and instead of populating the correct cell it will populate the ordinal cell corresponding to the index.

document.getElementById("myTR").cells['9'].innerHTML = "testValue"

//the following doesn't work either 
a ='9'  or  a = 9
b = a.toString()
document.getElementById("myTR").cells[b].innerHTML = "testValue"

Any ideas how to solve this? I've tried to aggregate a letter to the number (9n) and it works, I just wonder if there is any know procedure for this.

2
  • It's working here. Commented Oct 19, 2012 at 9:44
  • Yes but I really need to call it by name not index Commented Oct 19, 2012 at 10:24

4 Answers 4

2
document.getElementsByName('9')[0].innerHTML = 'testVal';

gets elements by name, selects the first (only) element and Bob's your uncle.

Sign up to request clarification or add additional context in comments.

1 Comment

I think that wen you call "getElementById()" the result is an object with the elements inside it, then you cant call a getElementsByName() on it it throws an error "getElementsByName is not a function"
1

You may try this

HTML

<table>
    <tr id="myTR">
        <td>Row1</td><td name="9">Row1</td>
    </tr>
    <tr><td>Row2</td><td>Row2</td></tr>
</table>

JS

var x='9';
document.getElementsByName(x)[0].innerHTML = "testValue";

DEMO.

Also try to avoid to declare names that begins with numbers.

3 Comments

don't need to declare it before, "document.getElementsByName('9')[0].innerHTML = 'testValue'" works fine, thanks!
Just one question, what does the '[0]' do?
I guess it is the first element found with the provided name right?
1

try this if the name is unique:

document.getElementByName('9').innerHTML = 'testValue';

by the way, I suggest u use jquery to operate the cell, follow is the code use jquery:

$('td[name=9]','#myTR').html("testValue");

4 Comments

"document.getElementByName('9').innerHTML = 'testValue'" ' doesn't work but "document.getElementById('9').innerHTML = 'testvalue'" works
the name is unique, but it doesn't work, no error but it's not changing anything
"document.getElementsByName('9')[0].innerHTML = 'testValue'" this works
Sorry, I forget standard js do not like jquery can detect if it is a list
1

Not sure if I will get frowned upon by JS purists, but I suggest using jQuery for this, this is what it was built for:

$("#myTR TD[name='9']").html("testValue");

This should get the cell named 9 in the tr tag with id myTR

Heres a fiddle for you: http://jsfiddle.net/aZEXV/1/

3 Comments

That's exactly what he didn't want :)
jQuery is still JavaScript, usually if they dont want to use jQuery, they state that explicitly. :)
Well, thanks Jeff but this is exactly the problem, I don't want to get the 9th cell, I want to reach a cell named '9' and I really don't want to use jQuery for something so simple.

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.