ComboBox - Can't Show Hide Div! - Please help!
I've simply added this code to the original "Autocomplete combobox" JQuery UI
$('#combobox').change(function(){
if($('#combobox').val() == 'sftp') {
$('#sftp_div').show();
} else {
$('#sftp_div').hide();
}
if($('#combobox').val() == 'as2') {
$('#as2_div').show();
} else {
$('#as2_div').hide();
}
});
$("#combobox").change(function() {
alert(this.value);
});
});
When you click the button "show underlying select", the show/hide logic for the div works.
(I don't even want the toggle functionality to be able to show 'underlying')
This is the complete code:
##########################################
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AutoComplete JQuery UIt</title>
<link rel="stylesheet" href="themes/base/jquery.ui.all.css">
<script src="jquery-1.10.2.js"></script>
<script src="ui/jquery.ui.core.js"></script>
<script src="ui/jquery.ui.widget.js"></script>
<script src="ui/jquery.ui.button.js"></script>
<script src="ui/jquery.ui.position.js"></script>
<script src="ui/jquery.ui.menu.js"></script>
<script src="ui/jquery.ui.autocomplete.js"></script>
<script src="ui/jquery.ui.tooltip.js"></script>
<link rel="stylesheet" href="css/demos.css">
<style>
.custom-combobox {
position: relative;
display: inline-block;
}
.custom-combobox-toggle {
position: absolute;
top: 0;
bottom: 0;
margin-left: -1px;
padding: 0;
/* support: IE7 */
*height: 1.7em;
*top: 0.1em;
}
.custom-combobox-input {
margin: 0;
padding: 0.3em;
}
</style>
<script>
(function( $ ) {
$.widget( "custom.combobox", {
_create: function() {
this.wrapper = $( "<span>" )
.addClass( "custom-combobox" )
.insertAfter( this.element );
this.element.hide();
this._createAutocomplete();
this._createShowAllButton();
},
_createAutocomplete: function() {
var selected = this.element.children( ":selected" ),
value = selected.val() ? selected.text() : "";
this.input = $( "<input>" )
.appendTo( this.wrapper )
.val( value )
.attr( "title", "" )
.addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
.autocomplete({
delay: 0,
minLength: 0,
source: $.proxy( this, "_source" )
})
.tooltip({
tooltipClass: "ui-state-highlight"
});
this._on( this.input, {
autocompleteselect: function( event, ui ) {
ui.item.option.selected = true;
this._trigger( "select", event, {
item: ui.item.option
});
},
autocompletechange: "_removeIfInvalid"
});
},
_createShowAllButton: function() {
var input = this.input,
wasOpen = false;
$( "<a>" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.tooltip()
.appendTo( this.wrapper )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "custom-combobox-toggle ui-corner-right" )
.mousedown(function() {
wasOpen = input.autocomplete( "widget" ).is( ":visible" );
})
.click(function() {
input.focus();
// Close if already visible
if ( wasOpen ) {
return;
}
// Pass empty string as value to search for, displaying all results
input.autocomplete( "search", "" );
});
},
_source: function( request, response ) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
response( this.element.children( "option" ).map(function() {
var text = $( this ).text();
if ( this.value && ( !request.term || matcher.test(text) ) )
return {
label: text,
value: text,
option: this
};
}) );
},
_removeIfInvalid: function( event, ui ) {
// Selected an item, nothing to do
if ( ui.item ) {
return;
}
// Search for a match (case-insensitive)
var value = this.input.val(),
valueLowerCase = value.toLowerCase(),
valid = false;
this.element.children( "option" ).each(function() {
if ( $( this ).text().toLowerCase() === valueLowerCase ) {
this.selected = valid = true;
return false;
}
});
// Found a match, nothing to do
if ( valid ) {
return;
}
// Remove invalid value
this.input
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
this.element.val( "" );
this._delay(function() {
this.input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
this.input.data( "ui-autocomplete" ).term = "";
},
_destroy: function() {
this.wrapper.remove();
this.element.show();
}
});
})( jQuery );
$(function() {
$('#sftp_div').hide();
$('#as2_div').hide();
$( "#combobox" ).combobox();
$( "#toggle" ).click(function() {
$( "#combobox" ).toggle();
});
$('#combobox').change(function(){
if($('#combobox').val() == 'sftp') {
$('#sftp_div').show();
} else {
$('#sftp_div').hide();
}
if($('#combobox').val() == 'as2') {
$('#as2_div').show();
} else {
$('#as2_div').hide();
}
});
$("#combobox").change(function() {
alert(this.value);
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label>Protocol: </label>
<select name="combobox" id="combobox">
<option value="select">Select one...</option>
<option name="sftp" value="sftp">SFTP</option>
<option name="as2" value="as2">AS2</option>
</select>
</div>
<div name="sftp_div" id="sftp_div" style="background: #ccc;">SFTP content in there</div>
<div name="as2_div" id="as2_div" style="background: #ccc;">AS2 content in there</div>
<button id="toggle">Show underlying select</button>
</body>
</html>
##########################################
Thank you.