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

2

3 Answers 3

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

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

2 Comments

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
Yes you are right, I forgot to wrap this code with a function :)
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.

Comments

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/

3 Comments

I want id by attribute name ie sym_name
@Warrior Updated but I'm not sure if you will like the answer. I don't think there is a native way to retreive elements by a custom attribute.
How i can loop through the iput types with id starting with 'inp__strObjectType__'

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.