tags:

views:

47

answers:

4

I have the following dropdown list box:

      <select name="DDLConditional1" size="1">
            <option selected="selected">is equal to</option>
            <option>begins with</option>
            <option onclick="ShowBetween();">is between</option>
            <option>is not equal to</option>
            <option>is greater than</option>
            <option>is less than</option>
        </select>

I want to show() a textbox and a label based on someone selecting the "is between" option of the dropdown list. I have multiples of these dropdowns and all of them will have to do the same thing. In other words I have DDLConditional1 2 3 4... infinite. I've already been able to make the button work that appends new conditionals.

A: 

You don't want onclick - you want to use the onchange of the <select>, where you'll test its current value to see if it's what you want.

mway
Won't work in 1.3.2 and IE.
Stefan Kendall
I don't recall anything about 1.3.2, and I also don't recall IE complaining about `onchange`. Next time you disagree with a group of answers, post a better solution instead of being unhelpful.
mway
Thank you all of you for your prompt responses. The ".live" solution works for me, and on IE8. My next challenge is to show two dynamically created and enumerated controls (one label and one text box) based on the "is between" being selected.
DeanoReno
+1  A: 

Why not use jQuery throughout with the onChange event instead of onClick:

jQuery(document).ready(function() {    

   jQuery("select[name^=DDLConditional]").live("change", function() {
      if(jQuery("option:selected", this).text() === "is between") {
         //display the textbox and label
      }
   });
});

This code runs after the page has loaded. It looks for all select elements whose names start with DDLConditional. It then uses live (so that even future elements that could be added dynamically are considered) to bind a handler to the change event.

Vivin Paliath
Won't work in 1.3.2 and IE.
Stefan Kendall
@Stefan What are you talking about? Where does jQuery 1.3.2 and IE even come into this? That's why you downvoted? Please don't downvote without valid reason.
Vivin Paliath
Apparently @Stefan thinks we're not allowed to give answers that aren't compatible all the way back to... what... `jQuery version 1`?
patrick dw
I've flagged his comments as noise.
Marko
@patrick. Apparently. I wish people would spell out their disagreement. This kind of mass downvoting is completely unhelpful; even seems spiteful.
Vivin Paliath
Vivin - Agreed. Seems especially spiteful since @Stefan didn't stick around to enter into any discussion. More like a hit 'n run.
patrick dw
+1  A: 

I'd get rid of the inline handler assignment, and use jQuery to manage your handler.

It sounds like the <select> elements are being dynamically added to the DOM, so this uses jQuery's .live() method to handle those dynamic elements.

Example: http://jsfiddle.net/GPMmJ/

$(function() {
    $('select[name^=DDLConditional]').live('change', function() {
        if( $(this).val() === 'is between') {
            alert('is between was selected');
        }
    });
});

Any time a <select> that has a name that starts with DDLConditional is added to the page, the change handler will work automatically. It gets the value of what was selected using jQuery's .val() method, and checks to see if it is the in between one.

patrick dw
You want 'click', not 'change'. This won't work in 1.3.2 and ie.
Stefan Kendall
@Stefan - So `jQuery 1.3.2` is being used here? That's why you down-voted me? Because I really don't see that anywhere in the question.
patrick dw
@Stefan, You're an idiot. It DOES work in IE6/7/8 and the OP never mentioned jQuery 1.3.2. http://jsfiddle.net/JhBdm/. Now give me my vote back :)
Marko
@patrick wouldn't `val()` simply return a blank string? I think you want the `text()` of the option :)
Vivin Paliath
@Vivin - Actually, no. When there isn't a value assigned to the `<option>`, the `value` property defaults to the text content. The only issue is that the `value` property for IE6 doesn't do this, but jQuery's `.val()` corrects it. :o)
patrick dw
He really should use the `value` attribute versus relying on the `<option>` text, but to each their own. Also: patrick's last comment.
mway
@patrick @mway oh! I did not know that! Learned something new today :)
Vivin Paliath
+1  A: 

Yep sure thing, just wrap the <select> in a div and you can easily append a textbox using jQuery. Something like:

<div class="conditional>
    <select name="DDLConditional1" size="1">
        ...
    </select>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        // store our select as a jQuery object
        var $select = $(".conditional select");
        $select.change(function() {
            // get our parent (the div)
            var $wrapper = $(this).closest(".conditional");

            // if the selected option's text is "in between"
            if($("option:selected", this).text() == "is between") {
                // append a textbox
                var $newTextbox = $("<input type='text' />");
                $newTextbox.css('color', 'red'); //perhaps set some CSS properties?
                $wrapper.append($newTextbox);
            }
        });
    });
</script>

Here's an example that works in IE 7/8, Firefox, Chrome and Safari.

Marko