user2286693
user2286693

Reputation: 3167

Trigger event in HTML <select> box when the same entry is selected again

In a HTML form, I use a drop-down list like this:

<select onchange="send()">
  <option value="a">Option A</option>
  <option value="b">Option B</option>
  <option value="c">Option C</option>
</select>

When the user changes the selected entry, send() is called, which basically uses jQuery.ajax(...) to send the new value to the server. This works fine.

When the transmission fails for some reason, the user is informed about the error. Most users will then select the same entry again to retry. Now the problem is that this will not trigger the onchange event again because the value hasn't changed. What would be the best way to deal with this?

Upvotes: 0

Views: 1062

Answers (3)

yovie
yovie

Reputation: 79

maybe you can use onclick event

Upvotes: 0

Sridhar R
Sridhar R

Reputation: 20418

Try this HTML

<select id="chg">
    <option>a</option>
    <option selected>b</option>
    <option>c</option>
</select>

Script

$("select#chg").mouseup(function() {
    var open = $(this).data("isopen");

    if(open) {
        alert($(this).val());
    }

    $(this).data("isopen", !open);
});

DEMO

Upvotes: 2

mujaffars
mujaffars

Reputation: 1483

You can create a empty value option

<option value=''></option>

Which is by default selected as first load, When error occurs for any selection you can select 'empty' option from select box. So for next time user will need to selection option and your 'send()' function will call.

// Selection empty option by code
$('#selectBoxId').val('');

Upvotes: 1

Related Questions