1

I am using js to , onclick of a checkbox, it enables or disables text input.

It works, until I put it into a live form with google jquery api.

Weird but.. must be a conflict somewhere.

The form element is: ( code isnt adding to this post properly )

<input type="checkbox" name="others" onclick="enable_text(this.checked)" class="medium" /></div>

Name on credit card if different from above

The js is:

function enable_text(status)
{
status=!status;
document.form1.other_name.disabled = status;
}

What am I doing wrong, I have used body onload handler.

<body onload=enable_text(false);>

JS FIDDLE : http://www.jsfiddle.net/ozzy/H8VPY/4/

3 Answers 3

4

Here, a jQuery solution:

$("input:checkbox").click(function() {
    $("input:text").attr("disabled", !this.checked); 
});

Just replace these generic selectors with your more specific ones.

In addition to this code, you will probably also want to make the text-box disabled (initially).

<input type="text" disabled="disabled" />

Working demo: http://www.jsfiddle.net/H8VPY/11/

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

4 Comments

I want the form element to be editable when ticked, disabled when not ticked
works great, chosen as answer, but need to attribute to specific ID. I will sort that thanks
Still struggling to apply this to the input ID, we must specify the ID, otherwise other input fields remain disabled/enabled
@422 Use the ID selector: $("#textboxID").attr(...);
3

There are several problems in your jsfiddle demo.

  • The <script> and the comment there around should be removed.
  • A <form name="form1"> is missing which caused document.form1 to return nothing.
  • The onLoad option on left menu should be a no wrap (head) since it's just a function.

Updated demo: http://www.jsfiddle.net/PPZYm/

3 Comments

Adding the disabled="disabled" attribute to the text-box will suffice. That way, the text-box will initially be disabled.
@Šime: Yes, can also, if that's the actual intent. I only wonder why the OP didn't include it in his own demo.
Apologies for confusion, I had stripped some code to make it simpler and inadvertantly cocked it up. @Sime your code works A1 thanks.
2

Live Example

function enable_text(status) {
    status = (status) ? false : true; //convert status boolean to text 'disabled'
    document.form1.other_name.disabled = status;
}

Note

You also need to wrap your div in the jsfiddle example with a <form> tag with the name form1 for it to properly work

<div class="controlset-pad">
    <input type="checkbox" name="others" onclick="enable_text(this.checked)" class="medium" />
</div>
<form name="form1">
    <div class="field4">
        <label>Name on credit card if different from above</label><input type="text" name="other_name" class="medium" disabled="disabled" />
    </div>
</form>

3 Comments

I need the element to be disabled until ticked
You don't need that first line in the function body. ... = !status; works just fine.
thanks but, disabled="disabled" remains disabled when we run it

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.