0

I have written following code in html:

<input type="text" id="id_1" name="text_1">
<input type="text" id="id_2" name="text_2">
<input type="text" id="id_3" name="text_3">

Here I have to get all textBoxes in an array in javascript function whose id starts with "id". So, that I can get above two textBoxes in an array.

How to get all textBoxes whose id start with "id"?

1
  • you can simply define name and id as an array "text_1[]" so through this can you can access.. in java script just define $('#field_name').val() Commented May 24, 2013 at 9:46

4 Answers 4

3
var nodeList = document.querySelector("input[name^='text_'")

A nodeList should be sufficiently like an array for your purposes.

Note that support for querySelector might not be sufficient for your purposes (in which you will need to getElementsByTagName and then filter the results in a loop).

Alternatively you could use a library which provides its own selector engine. In YUI 3 you could:

var listOfYUIObjects = Y.all("input[name^='text_'");

Mootools, Prototype, jQuery and a host of other libraries provide similar functionality.

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

2 Comments

I have changed my question please give me answer of edited question.
@rishikeshtadaka — Replace "name" and "text" with "id" in the query.
0
var ele = document.getElementsByTagName("input");
var matchingEle = [];
var eleName = '';

for (var i = 0; i < ele.length; ++i) {
 el = ele[i];
 eleName = el.getAttribute("name");
 if (eleName && eleName.indexOf("text_") == 0) {
     matchingEle.push(el);
 }
}

Comments

0

You could use a generic function that filters a list of elements based on a pattern. This is useful if you want to do a similar thing in future but with different criteria on the properties.

http://jsfiddle.net/3ZKkh/

function filter(elements, pattern) {
    var i, j, match, e, f = [];

    for (i = 0; i < elements.length; i += 1) {
        e = elements[i];
        match = true;

        for (j in pattern) {
            if (pattern.hasOwnProperty(j)) {
                if (!(j in e && pattern[j](e[j]))) {
                    match = false;
                    break;
                }
            }
        }

        if (match) {
            f.push(e);
        }
    }

    return f;
}

var pattern = {
    'type': function (t) {
        return t.toLowerCase() === 'text';
    },
    'name': function (t) {
        return t.toLowerCase().search('text') === 0;
    }
};

console.log(filter(document.getElementsByTagName("input"), pattern));

Comments

0
var list = document.getElementsByTagName('input');  //Array containing all the input controls
var textBoxArray = [];                              //target Array
for (var i = 0; i < list.length; i++) 
{
    var node = list[i];

    if (node.getAttribute('type') == 'text' &&  node.getAttribute("id").substring(0, 1) == "id") 
    {
     /*
          insert matching textboxes into target array
     */
      textBoxArray.push(node);
    }
} 

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.