views:

29

answers:

3

Testing part of a form. So, right now I just want to alert what the user selects:

JS:

function getData(title)
{
     alert(title);
}

HTML generated by PHP:

<select name="currentList" onChange="getData(this);">
     <option value="hat">Hat</option>
     <option value="shirt">Shirt</option>
     <option value="pants">Pants</option>
</select>

when I change the value I get an alert with:

[object HTMLSelectElement]

+1  A: 

try alert(this.value)

meder
that was it! Been doing a lot of AJAX so the small stuff starts to escape me. lol! I'll Accept the answer once the time limit runs out. Thanks!
dcp3450
A: 

With this you're passing the HTML dropdown element to the function, not the value of the selected option. To obtain the value of the selected option, you need to get the selected option from the options by selectedIndex and then get its value. In a nut:

function getData(dropdown) {
    var value = dropdown.options[dropdown.selectedIndex].value;
    alert(value);
}
BalusC
A: 

With jQuery, you could be concise with alert($("#currentList").val());, as long as you add the id="currentList" to the select list. This way you don't have to pass "this" to the onchange function.

Ian Davis
i tried that first. Issue is my form is being created dynamically. Jquery would handle it only if my select box was static on the page. Once I made it part of a dynamically changing form it stopped recognizing it.
dcp3450