Hi genii, I have an input type set as text however I want this textbox to set to “Out Now” and greyed out (or do either of the two but both is preferable) when the user ticks on a separate checkbox. Can anyone help?

Pepejeria
—
2008-02-22T15:45:53Z —
#2

This is pretty basic JavaScript. Please show what you got so far and I will help you out.

Bravogolf
—
2008-02-22T16:27:07Z —
#3

Thank you for the reply, I found out the answer from here in the meantime :rolleyes:

Paul_Wilkins
—
2008-02-22T20:28:27Z —
#4

Well that is pretty horrible code. Why is it so bad?

It has a slew of id values strewn about all over the place

There are identical id's! They're called unique identifiers for a good reason

Some attributes aren't lowercased, and are even inconsistant with each other

presentational markup has been used, those breaks need to go

the form elements aren't even contained within fieldset tags or at least, block elements like a paragraph

As for the scripting, it has scripting code mixed in with the content! You've no doubt been taught that CSS styles inline with the HTML is a bad thing, well scripting is also bad when inline with HTML. Keep the separation between the content, the presentation and the behaviour.

That will need to change completely, to be of any use. Yes it is possible for you to use that code but your page is going to be a lot worse off in the long run.

Get rid of all the id elements and have one only on the form itself. No others need to be used.

Sanitise the tag and attribute names

Remove the disabled attribute, which will be set only from the script itself

Remove the class name, which likewise will only be set from the script

Remove the break tags, they shouldn't be used in forms, and replace them with separate paragraphs. If you want less of a gap between the form paragraphs then that's presentational, do it from CSS instead.

The first checkbox has no name because you don't necessarily want to send it's state to the server, just the changes that it makes to the other ones.

Now when you activate one of the text checkboxs, you want it to toggle the state of the textbox that's beside it.

We will want an identifier on the form to make it easier for the script to find it, and a class name called "toggle" on each checkbox that we intend to use to toggle the input field. This is a good safety measure

This script gets loaded after the forms become available. The best way to do this is to place the script at the bottom of the page. That is also the best place to put scripts for page loading speed and optimization.

With that one id value on the form to help us find it, we can easily search the form and add an event to the appropriate checkboxes.

// get the form input elements
var form = document.getElementById('myForm');
var inputs = form.getElementsByTagName('input');
// walk through each input element
for (var i = 0; i < inputs.length; i++) {
// if it's a toggle element, assign an onclick event to it
if (inputs[i].className.match(/\\btoggle\\b/)) {
inputs[i].onclick = toggleInput;
}
}

So that's nice and easy. You walk through each form element and if it has the "toggle" classname, it gets an event attached to it.

The only bit that might need further explaining is the match part. That's using a regular expression to find the word "toggle". The regular expression is demarked with forward slashes (/.../) and the \b in there ensures that there is a word break, either a gap of some kind or the start/end of the string, which effectively means that it won't match "toggled" but only "toggle". You can also have multiple class names on that element, such as "toggle required" without causing any problems.

So now, when the checkbox is pressed, it calls a function called toggleInput(). Because the traditional event registration was used, the this keyword will refer to the element that was clicked. If the onclick attribute was used instead, we would be forced to pass the this keyword as an argument to to the function instead, and we might also be tempted to pass a target id name.

As I was saying we do not want to code that way. Instead, the this keyword is automatically passed when the event is assigned from javascript, and we can easily use DOM traversal techniques to find the input element that we want to toggle.

When the field gets disabled, IE doesn't provide much visual feedback on whether something is disabled or not, so we'll also add a class name of "disabled" when appropriate.

.disabled {
background: lightgrey;
}

Note: IE is funny. You have to spell it "lightgrey" or "gray". Other variations of spelling grey/gray aren't accepted. It would have been a lot better to use input[disabled="disabled"] so that we don't have to set the class name from the script, but IE can't get that right either.

Anyway, what follows is the workhorse of our code. It walks through each element after the checked one, and enables or disables the first appropriate input that it comes across.