• 0

[PHP/Ajax] ComboBox "SelectedItem change" event


Question

  • 0

You could go with an AJAX solution or just JS. If you want to do AJAX, it'll require downloading the content as the options are selected; with just JS, all the HTML will be loaded and displayed accordingly using CSS (initially).

For JS, based on the selected item, you would display a specific div while hiding the other ones.

Are you using a JS framework?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

I'm not using JS framework.....

but I think I little figure out how it will look like, but the code still doesn't work :(

<Option VALUE="Item3" onChange="Show();">3</option>

Where Show() is a JS function defined, I don't know how to deal with a combobox throught the function, how could I tell it:

If cboAct.Selecteditem = "Item3" then MyDiv.visible = false ........ something like that

I still didn't tested if onChange event works with each item not for the entire combobox.

Link to comment
Share on other sites

  • 0

Thanks all for your help, I managed to do it finally, It will be as follows :)

&lt;script language="JavaScript"&gt;&lt;!--
function T1(object) {
if (object == "Item3")
	{
	document.getElementById('myId').style.visibility = 'visible';
	}
	else
	{
	document.getElementById('myId').style.visibility = 'hidden';
	}
}
//--&gt;&lt;/script&gt;

"myId" is an ID of the div, I can put in it whatever I want (e.g. textbox, button) , it's like a panel for child controls, so once it's hidden, all childs will be hidden as well.

&lt;Select NAME="cboAct" onChange="T1(this.value);" style="width:200px;"&gt;

T1 is function and "this.value" gets the current selected item.

Thanks again

Link to comment
Share on other sites

  • 0

When a <select> option is chosen then strictly the value property of the <select> isn't assigned (but might be to allow for "shorthand" script such as yours) but the selectedIndex property is. You then interrogate the options object in the <select> to get the value (or text) of the actual selected option.

That code looks like something a IE-centric IDE would spew out BTW...

&lt;script type="text/javascript"&gt;
function showPanel(sel){
	var el=document.getElementById('myId');
	el.style.visibility=(sel.options[sel.selectedIndex].value=='Item3')?'visible':'hidden';
}

&lt;select name="cboAct" style="width:200px;" onchange="showPanel(this);"&gt;
	&lt;option value="Item1"&gt;1&lt;/option&gt;
	&lt;option value="Item2"&gt;2&lt;/option&gt;
	&lt;option value="Item3"&gt;3&lt;/option&gt;
 &lt;/select&gt;

FWIW: <select />s aren't comboboxes. Comboboxes are extended <select />s that allow a text entry as well as selecting from a list (the dynamic Google search is a commo example). The Microsoft (and others) terminology for a <select> is "dropdown".

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.