0

I need to figure out how to make my javascript take in the select names dynamically. How should I go about this?

I've tried Tried var i = getElementsByName() and then if (i == "x) ... else .... with no results

My Javascript:

function displayResult()
{
   var m=document.getElementsByName("x");
   document.getElementById('divid').innerHTML = m[0].value;
}                                   

My Html:

  <form>
       <select name = x>
         <option> a </option>
         <option> b </option>
         <option> c </option>
       </select>
       <select name = y>
         <option> 2 </option>
         <option> 3 </option>
         <option> 4 </option>
       </select>
     </form>
8
  • @pure_code Need document.getElementsByName("x"); to get the names dynamically Commented Feb 8, 2013 at 18:18
  • do you mean you want "x" to be a variable? Commented Feb 8, 2013 at 18:18
  • Obligatory : What have you tried? Commented Feb 8, 2013 at 18:18
  • @AshwinMukhija Tried var i = getElementsByName() and then if (i == "x) ... else .... No results Commented Feb 8, 2013 at 18:20
  • The "ByName" in getElementsByName() referrers to the element name (like <select> or <form>) not the name attribute. In case that helps. Commented Feb 8, 2013 at 18:22

3 Answers 3

2

The simplest solution would be with jQuery http://jquery.com

var myx = "x";
var elems = $("select[name=" + myx + "]");

Also with new browser APIs using pure Javascript:

var elems = document.querySelectorAll("select[name=x]")

https://developer.mozilla.org/en-US/docs/DOM/Document.querySelectorAll

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

3 Comments

X needs to be dynamic. We dont know if its gonna be x, y, z or what
Updated the answer. You can dynamically create strings in Javascript.
If you are not familiar dealing with strings in Javascript I suggest you study Javascript basics as the scope of the additional question is in the learning the programming language basics codecademy.com/tracks/javascript
1

So you need that x to be 'dynamic'. I don't really understand what you mean with that but I guess you want to specify another name every time you call the function. That's what function parameters are for.
Change your function to this:

function displayResult(name)
{
   var m=document.getElementsByName(name);
   document.getElementById('divid').innerHTML = m[0].value;
}

Now you can call the function with:

displayResult("x");

2 Comments

Exactly what I needed. I dont know why I didnt think of it this way. So simple. Long day at work...
Do note that if you have more elements with the same name this method only sets the first. If you need all of them you should change the function to include looping over all these elements, if you want to decide that on every function call add a second parameter to use as the array index. @JeftLebowski
0

I think that what you are actually looking for is this:

var nodes = document.getElementsByTagName('select');
var names = new Array();
for(i=0,c=nodes.length;i<c;i++){
    names.push(nodes[i].name);
}

this code will produce an array names containing the names of selects presented on the page

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.