Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery change function not working

I can't seem to get this working seems like it should

$('.titleother').change(function() {

if ($('.titleother').val() == 'Other') {
    ​$('.hiddentext').css('display','block')
}​​​

})

For this HTML

<select class="titleother">​
<option value="1">1</option>
<option value="2">2</option>
<option value="Other">Other</option>
</select>

<p class="hiddentext" style="display:none">TEXT</p>

Any ideas?

like image 631
Jamie Taylor Avatar asked Oct 25 '10 08:10

Jamie Taylor


People also ask

How to work on change in jQuery?

The change() method triggers the change event, or attaches a function to run when a change event occurs. Note: For select menus, the change event occurs when an option is selected. For text fields or text areas, the change event occurs when the field loses focus, after the content has been changed.

Is not a function error jQuery?

on if not a function" jQuery error occurs for 2 main reasons: Loading an old version of the jQuery library that doesn't support the on function. Loading a library that overrides the value of the dollar sign $ variable.

How do I get the select box value?

There are two methods of getting the value of the selected option. You can either select text or find the position of it in a drop-down list by using the option:selected attribute or the val() method in jQuery. The val() method returns the value of selected attribute value.


2 Answers

This works for me using Chrome:

$(function(ready){
    $('.titleother').change(function() {
        if ($(this).val() == 'Other') {
            $('.hiddentext').show();   
        }
    });
});

http://jsfiddle.net/amuec/11/

(Darin's code didn't work for me either)

like image 198
Marwelln Avatar answered Sep 23 '22 13:09

Marwelln


Make sure you put this in a $(document).ready so that the DOM is ready when you attach the .change() handler:

$(function() {
    $('.titleother').change(function() {
        if ($(this).val() == 'Other') {
            ​$('.hiddentext').show();
        }​​​
    });
});
like image 27
Darin Dimitrov Avatar answered Sep 23 '22 13:09

Darin Dimitrov