user3767853
user3767853

Reputation: 293

How to check select tag value

I currently have a table with a bunch of options with select tags like so --

This lets you select the 'OWNER' of the thread:

        <tr>
          <th width=""><span class="owner">Owner:</span></th>
          <td>
            <span id="task_owner">
              <input type="hidden" name="orig_owner" id="orig_owner" value="<%= $task->owner %>" />
              <select id="owner_sel" name="owner"<% print ' style="display: none"' if(!$user->has_permission($task->cid, $PERMISSION_CHANGE_OWNER)); %>
                onChange="if(this.form.state_id.value == <%= $STATE_NEW %>) this.form.state_id.value = <%= $STATE_ASSIGNED %>">

                <option></option>
                <% 
                   foreach my $h (@sorted_users){
                   foreach my $uid (keys %{$h}) {
                   my $selected = ($uid == ($form->{owner} || $task->owner)) ? 'selected="selected"' : '';
                print qq{<option value="$uid" $selected>$h->{$uid}</option>};
                }
                }
                %>
              </select>
              <span id="owner_txt"<% print ' style="display: none"' if($user->has_permission($task->cid, $PERMISSION_CHANGE_OWNER)); %>><%= $users->{$task->owner} %></span>
            </span>
          </td>
        </tr>

and this is the 'STATE' of the thread:

        <tr>
          <th width=""><span class="state">State:</span></th>
          <td>
            <input type="hidden" name="orig_state_id" id="orig_state_id" value="<%= $task->state_id %>" />
            <select id="state_id" name="state_id"
              <% print ' style="display: none"' if(!$user->has_permission($task->cid, $PERMISSION_CHANGE_STATE)); %>
              <% if ($task->state_id == $STATE_CLOSED){ %> onchange="stateChanged(this.value, <%=$STATE_CLOSED%>)" <% } %>>

              <% foreach(@states) { %>
              <option value="<%=$_->{STATE_ID}%>" <%= $_->{STATE_ID} == ($form->{state_id} || $task->state_id) ? 'selected="selected"' : "" %>><%=$_->{STATE}%></option>
              <% } %>
            </select>
            <span id="state_txt"<% print ' style="display: none"' if($user->has_permission($task->cid, $PERMISSION_CHANGE_STATE)); %>><%= $current_state %></span>
          </td>
        </tr>

Both of these have their select tags populated from a database. Currently the 'STATE' is set to change from 'NEW' to 'ASSIGNED' if any changes are made to the 'OWNER'.

I want to make it so that if someone changes the 'STATE' of the thread from 'ASSIGNED' to 'NEW' then it will go into the 'OWNER' and change it to the default, which is just an empty string " ".

I also want to make it so that if the user selects the " " from the 'OWNER' then it will automatically change the 'STATE' of the thread from 'ASSIGNED' to 'NEW'.

My question is:

Is it possible to use javascript(or whatever better choice there is) to check for a specific select value, or what the user has selected? If so what is the best approach.

I'm a very visual learner so if you can show me an example that would be perfect.

Upvotes: 0

Views: 720

Answers (1)

Jaime Gomez
Jaime Gomez

Reputation: 7067

This will get you started:

state_id.onchange = function() {
    if (this.value == 'new') {
        owner_sel.selectedIndex = 0
    }
}

You should be able to figure out the rest :)

I would also recommend separating the script from the html, it'll make things easier to read and maintain. So instead of doing this:

<select id=something onchange="..."></select>

Add this at the end of the body:

<script>
    something.onchange = function() { ... }
</script>

Just my 2cents :)

Upvotes: 2

Related Questions