Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you getElementsByName if you only have partial name on javascript?

I have a repeating table where the name of the elements would be (e.g. 'tdName_1' 'tdName_2'), and I was wondering if it would be possible to getElementsByName('tdName_').

PS: I can not use Jquery.

Thanks In advance.

Cesar.

like image 586
Amra Avatar asked Feb 16 '10 11:02

Amra


People also ask

What is difference between GetElementByID and getElementsByName?

The difference is that GetElementByID() will retrieve a single element object based on the unique id specified, whereas GetElementsByTagName() will retrieve an array of all element objects with the specified tag name. Then you can obtain the value using GetElementById from your C++ code.

What is the getElementsByName () method commonly used to obtain?

The getElementsByName() method of the Document object returns a NodeList Collection of elements with a given name attribute in the document.

How do you get getElementsByName value?

JavaScript getElementsByName() example How it works: First, select the submit button by its id btnRate using the getElementById() method. Second, listen to the click event of the submit button. Third, get all the radio buttons using the getElementsByName() and show the selected value in the output element.


1 Answers

This is not possible. I'm assuming for the rest of this answer that the elements you're interested in are <td>s. If so, then you should be aware that the name attribute is not valid for <td> elements.

You will have to create a list of matching elements manually. If you decide to use the name attribute anyway (instead of, say, adding a class in the class attribute), something like the following will work:

var table = document.getElementById("your_table_id");
var tds = table.getElementsByTagName("td");
var matchingTds = [];

for (var i = 0, len = tds.length, td, tdName; i < len; ++i) {
    td = tds[i];
    tdName = td.getAttribute("name");
    if (tdName && tdName.indexOf("tdName_") == 0) {
        matchingTds.push(td);
    }
}
like image 93
Tim Down Avatar answered Sep 18 '22 15:09

Tim Down