0

I was trying to make this small script where you can only use numbers once in a series of dropdown boxes so.. [1] [3] [2] [2] [3] [1] [2] [1] [3] all are fine but not [2] [2] [1] [1] [1] [3] ..

I think I have it half way done.

$("#left").focus(function () {
    // Store the current value on focus, before it changes
    previous = this.value;
}).change(function() {
    // Do soomething with the previous value after the change
    var num1 = $("#left option:selected").text();

    if ( $("#middle option:selected").text() == num1){
    $("#middle option:selected").text(previous)
    }

    if ( $("#right option:selected").text() == num1){
    $("#right option:selected").text(previous)
    }       
});

Here is a link to the full thing: http://jsfiddle.net/U3WSz/

Its works for a little bit but I realized the drop down options started changing. If you play around with it enough, you'll notice what i'm seeing.

I also notice how I repeat a lot of code. Let me know if there's a better way to write it.

1
  • Did you look up what .text() does? Commented Mar 28, 2013 at 21:59

2 Answers 2

1

jsFiddle

add value attribute to option's like this

<option value='2'>2</option>

Js:

   $("#left").focus(function () {
    // Store the current value on focus, before it changes
     previous = $(this).val();

    }).change(function () {
    // Do soomething with the previous value after the change
    var num1 = $("#left option:selected").val();

    if ($("#middle option:selected").val() == num1) {
        // $('body').append(previous);
        $("#middle").val(previous)
    }

    if ($("#right option:selected").val() === num1) {
        $("#right").val(previous);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hey... I don't know why but after playing with it, it seems to mess up. Your code makes sense, but it doesn't seem to run it all the time. Try to play with just one box. You'll start seeing it duplicate itself.
0

Try this

$("#left").focus(function () {
    // Store the current value on focus, before it changes
    previous = this.value;
}).change(function() {
    // Do soomething with the previous value after the change
    var num1 = $("#left option:selected").text();

    if ( $("#middle option:selected").text() == num1){
    $("#middle option[value='"+previous+"']").attr('selected','selected')
    }

    if ( $("#right option:selected").text() == num1){
    $("#right option[value='"+previous+"']").attr('selected','selected')
    }       
});

This code makes selected the options, in #middle and #right select boxes, that have value equal to value of previous variable.

1 Comment

actually I've just realized that OP doesn't want to evaluate based on options value. No value attribute for <option>. This renders my answer being far from being valid, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.