Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript code for getting the selected value from a combo box

Can any one give me a sample code that gets the selected value from an existing combo box?

I have this code but its not doing anything:

function check () 
{
    var e = document.getElementById("ticket_category_clone");
    var str = e.options[e.selectedIndex].text;

    alert(str);

    if (str==="Hardware")
    {
        SPICEWORKS.utils.addStyle('#ticket_c_hardware_clone{display: none !important;}');
    }       
}

SPICEWORKS.app.helpdesk.ready(check);​

heres a img of the code

and the code

<select id="ticket_category_clone" name="ticket[category]" hdpp="ticket_category">
<option value=""></option><option value="Hardware">Hardware</option>
<option value="Software">Software</option>
<option value="Rede" selected="selected">Rede</option>
<option value="Pedidos">Pedidos</option>
<option value="Formação/Dúvida">Formação/Dúvida</option>
<option value="Outro">Outro</option><option value="#edit_categories#">Edit Categories...</option></select>

what i want its find a way to get the selected value fo that combobox

like image 525
user181891 Avatar asked Apr 04 '12 08:04

user181891


People also ask

How do I get the value of a select tag?

To get the value of a select or dropdown in HTML using pure JavaScript, first we get the select tag, in this case by id, and then we get the selected value through the selectedIndex property. The value "en" will be printed on the console (Ctrl + Shift + J to open the console).


1 Answers

There is an unnecessary hashtag; change the code to this:

var e = document.getElementById("ticket_category_clone").value;
like image 125
Willem D'Haeseleer Avatar answered Nov 03 '22 20:11

Willem D'Haeseleer