How to get an element by name and set its value

javascript, jquery

Solution

Description

You are mixing normal javascript and jQuery. Use the attribute selector.

Check out my sample and this jsFiddle Demonstration

Sample

Html

<input type="text" name="nameOfTheInputElement"/>

jQuery

$(function() {
    $("input[name='nameOfTheInputElement']").val("your value");
});
​

Edit

If you want, for some reason, change a element which name is a value in another element then do this. jsFiddle Demonstration

Html

<input type="text" id="questions" value="nameOfTheInputElement"/>    
<input type="text" name="nameOfTheInputElement"/>

​jQuery

$(function() {
    var name = $("#questions").val();
    $("input[name='"+name +"']").val("your value");
});​

More Information

- jQuery - Attribute Equals Selector [name="value"]

- jsFiddle Demonstration (first sample)

- jsFiddle Demonstration (second sample)

Problem

There should be a simple solution for this. I need to get an input element by name and set its value. The following Javascript does not work: ``` x = document.getElementsByName($('#questions').val()); x.value=this.value; ``` Is there a simple solution using JQuery?

Original source