Reputation: 5770
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/
Upvotes: 1
Views: 17802
Reputation: 8855
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>
Upvotes: 2
Reputation: 185913
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/
Upvotes: 4
Reputation: 1108692
There are several problems in your jsfiddle demo.
<script>
and the comment there around should be removed.<form name="form1">
is missing which caused document.form1
to return nothing.Updated demo: http://www.jsfiddle.net/PPZYm/
Upvotes: 3