Lovelock
Lovelock

Reputation: 8105

Changing form select input name when choosing a certain option

Trying to find a way of not posting an input to an array if a particular select option is selected and i can only think of this way with jquery.

What i currently use to change the class is:

document.getElementById("sel1").onchange = function () {
    this.className = (this.selectedIndex != 0) ? "selectoption-okay":"selectoption";
};

HTML:

<select id="sel1" class="selectoption" name="desc[]">
    <option selected="selected">Select an option...</option>
    <option value="1">Option 1</option>
</select>

And it works fine, but now what i want to do is change the name of the select input from 'desc[]' to e.g. 'dontuse'

I tried this:

 this.name = (this.selectedIndex != 0) ? "desc[]":"dontuse";

But it doesnt work, where am i going wrong?

Also is it possible to chain these type of conditional statements?

Upvotes: 0

Views: 59

Answers (1)

Oscar Paz
Oscar Paz

Reputation: 18312

In response to your second question, yes, it is possible to chain conditional statements:

var result = (condition) ? value1
                         : ((condition2) ? value2
                                         : value3);

Here, result will be value1 if condition is true, value2 if condition is false and condition2 true, and value3 if both condition1 and condition2 are false.

A conditional statement consists in a condition to evaluate and two expressions. But as a conditional statement is an expression in itself, you can chain them in very complicated ways (though they may become illegible if you chain too many).

Upvotes: 1

Related Questions