Click here to Skip to main content
15,922,407 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
HTML
<table>
    <tbody>
    <tr>
        <td>First name</td><td><input name="v[]" type="text" class="spec1" value="">
       </td>
    </tr>

    <tr>
        <td>Family name</td><td><input name="v[]" type="text" class="spec1" value="">
        </td>
    </tr>


in up code i have 2 input tags by same name ~> "v[]"
How Can to get Element Second Tag?!
if i get Elemnt By This Code :

JavaScript
document.getElementsByTagName("input")["v[]"].setAttribute('value', 'text');


then that work for first input Tag
-------------------------------
also how can getElemntByValue?
Posted
Updated 25-Mar-15 7:14am
v10
Comments
Sergey Alexandrovich Kryukov 25-Mar-15 13:32pm    
Tag or element itself? Inner HTML? Why?
Giving the same name to different elements makes no sense.
—SA

You can use document.getElementsByName to get all elements with a specific name. This returns an array, over which you can iterate:
JavaScript
var elems = document.getElementsByName("v[]");
for (var i = 0; i < elems.length; i++) {
    elems[i].setAttribute('value', 'text');
}


Quote:
also how can getElemntByValue?

This method does not exist. You'll have to write your own.
See http://stackoverflow.com/a/16527048[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Mar-15 13:41pm    
Right, a 5. I suggested the alternative, please see Solution 2.
—SA
nabeghe 25-Mar-15 14:03pm    
Thank you so much
My problem was resolved
Thomas Daniels 25-Mar-15 14:10pm    
You're welcome!
nabeghe 25-Mar-15 14:05pm    
var elems = document.getElementsByName("v[]");
elems[1].setAttribute('value', 'text');


i use this :) very thanks
I don't see how it could make any sense. Please see my comment to the question.

In all cases, you can easily navigate through DOM and find any elements by any features or their combination, or also by order of appearance, if you use jQuery:
http://api.jquery.com/category/selectors,
http://api.jquery.com/category/traversing,
https://api.jquery.com/each,
https://api.jquery.com/jquery.each (confusingly, under the same name, different each).

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery,
http://jquery.com,
http://learn.jquery.com,
http://learn.jquery.com/using-jquery-core,
http://learn.jquery.com/about-jquery/how-jquery-works (start from here).

If you don't want to use jQuery, you can download its source code and see how things you are interested in are achieved. :-)

—SA
 
Share this answer
 
v2
Comments
Thomas Daniels 25-Mar-15 13:44pm    
jQuery is a good suggestion, 5'ed.
Sergey Alexandrovich Kryukov 25-Mar-15 15:41pm    
Thank you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900