Bob J
Bob J

Reputation: 333

Disabled input text type with javascript

I'm having a little problem, i'm trying to make a disabled "input text type" enabled with a text in it.

function newForm(){
document.getElementsByName("name1").disabled = false;
document.getElementsByName("name1").value="TEST";
}

with this

<div id="passengers">
<div id="form">
    <table>
    <thead>
        <tr><th></th><th>Name</th><th>LastName</th><th>Date of Birth</th></tr>
    </thead>
    <tbody>
        <form id="formulaire" action="">
        <tr><th>Passenger #1</th><th><input type="text" name="name1" tabindex="1" disabled="true"/></th><th><input type="text" name="lastname1" tabindex="2" disabled="true"/></th><th><input type="text" name="date1" tabindex="3" disabled="true"/></th></tr>
    </table>
</div>
<input type="submit" value="New" onclick="newForm()"/><input type="submit" value="Send" disabled="true"/><input type="submit" value="Modify" disabled="true"/></form>
</div>

Upvotes: 0

Views: 3303

Answers (2)

dsgriffin
dsgriffin

Reputation: 68576

Firstly, change the input type of the button, from submit to button:

<input type="button" value="New" onclick="newForm()"/>

As your function has nothing to do with submitting the form (and submit will cause unwanted behaviour).

Secondly, getElementsByName returns a collection of objects, not one element. Target the element by accessing the first element using [0]:

function newForm(){
  document.getElementsByName("name1")[0].disabled = false;
  document.getElementsByName("name1")[0].value="TEST";
}

Here's a working jsFiddle.

Upvotes: 1

Devin Crossman
Devin Crossman

Reputation: 7592

getElementsByName returns a collection of elements try this

document.getElementsByName('name1')[0].disabled = true;

Upvotes: 1

Related Questions