Shinu Thomas
Shinu Thomas

Reputation: 5316

How to get the id of an input type using attribute

I have a check box like

<input id="inp__str_Object__17" type="checkbox" name="inp__str_Object__17" sym_name="Cust_attr_1" value="400024" >

Using javascript how can i get the id (inp__str_Object__17) using sym_name="Cust_attr_1"

Like document.getAttribute("SYM_NAME")

Upvotes: 2

Views: 147

Answers (3)

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

I'm not sure a native solution exists to retrieve an element by a custom attribute. You can do some of this work yourself such as:

var elem = document.getElementsByTagName("input");
var id = "";
for(var x = 0; x < elem.length; x++){
    if(elem[x].getAttribute("sym_name")){
       id = elem[x].id
    }
}

Working Example http://jsfiddle.net/WvaYx/1/

Or you could pull by the name attribute:

var elem = document.getElementsByName("inp__str_Object__17");
var id = elem[0].id;

Working Example http://jsfiddle.net/WvaYx/

Upvotes: 0

Pavel
Pavel

Reputation: 1647

var myInputs = document.getElementsByTagName('input');
for(var index in myInputs) {
   var myInput = myInputs[index].getAttribute('syn_name');
   if (myInput === 'some value')
      return myInputs[index].id;
}

Hope this was helpfull

Upvotes: 1

phtrivier
phtrivier

Reputation: 13415

You could use the kind of methods that do a "query by attribute" as described here Get elements by attribute when querySelectorAll is not available without using libraries? to get the input ; then, just get the "id" attribute.

Upvotes: 0

Related Questions