Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.change() get :selected text value

I'm trying to run the following code

$("select#shipping_rates_drop").change(function(){             var selectedText = $(this + "option:selected").text();             var ship = thisvalue.split("£");             var subtotal = $("ul#deliveryList").attr("class");             var ship = ship[1];             var totalcost = parseFloat(subtotal)+parseFloat(ship);             $(".wrap.form-section h1 span").html("&nbsp;&nbsp;&nbsp;&nbsp;<small>Total </small> £"+totalcost);             $("input[name=itemamt]").val(subtotal);             $("input[name=shippingamt]").val(ship);             checklistCheck1();         }); 

I want to get the text from the selected value on change. ex. UPS Worldwide Express - £89.57 then the code splits the value to get me the actual number of cost.

But console.log throws up the following

Error: Syntax error, unrecognized expression: [object HTMLSelectElement]option:selected

in jquery.min.js (line 2)

So im assuming I've done something wrong here and hoping someone could help out

like image 661
ngplayground Avatar asked Sep 05 '12 10:09

ngplayground


People also ask

How do you get the text value of a selected option?

Or to get the text of the option, use text() : $var = jQuery("#dropdownid option:selected"). text(); alert ($var);

How do I get the text value of a selected option in JavaScript?

function myNewFunction(element) { var text = element. options[element. selectedIndex]. text; // ... }

How do I get the select box value?

Answer: Use the jQuery :selected Selector You can use the jQuery :selected selector in combination with the val() method to find the selected option value in a select box or dropdown list.


2 Answers

the line should be

var selectedText = $(this).find("option:selected").text(); 
like image 135
Loken Makwana Avatar answered Oct 20 '22 15:10

Loken Makwana


Change

var thisvalue = $(this + "option:selected").text(); 

to

var thisvalue = $("select#shipping_rates_drop option:selected").text(); 
like image 44
psx Avatar answered Oct 20 '22 16:10

psx