2

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")

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
Warrior
  • 5,168
  • 12
  • 60
  • 87
  • 2
    Take a look here http://stackoverflow.com/questions/6267816/getting-element-by-a-custom-attribute-using-javascript – Irvin Dominin May 02 '13 at 09:12
  • http://stackoverflow.com/questions/9496427/how-to-get-elements-by-attribute-selector-w-native-javascript-w-o-queryselector – tbleckert May 02 '13 at 09:13

3 Answers3

1
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

Pavel
  • 1,627
  • 15
  • 12
  • you should to warp this code into `function` .. or assign id `var id;` and `break;` on condition match . other wise it throw error `SyntaxError: return not in function` – rab May 02 '13 at 09:39
  • Yes you are right, I forgot to wrap this code with a function :) – Pavel May 02 '13 at 09:41
0

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/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

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.

Community
  • 1
  • 1
phtrivier
  • 13,047
  • 6
  • 48
  • 79