44

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

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
ngplayground
  • 20,365
  • 36
  • 94
  • 173

4 Answers4

133

the line should be

var selectedText = $(this).find("option:selected").text();
JRichardsz
  • 14,356
  • 6
  • 59
  • 94
Loken Makwana
  • 3,788
  • 2
  • 21
  • 14
7

Change

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

to

var thisvalue = $("select#shipping_rates_drop option:selected").text();
psx
  • 4,040
  • 6
  • 30
  • 59
6

Selected text value :

$('#selectBoxId').on('change',function(){
   var optionsText = this.options[this.selectedIndex].text;
   alert(optionsText);
});
Hasib Kamal Chowdhury
  • 2,476
  • 26
  • 28
0
<select onchange="changeSelect($event)">
    <option [value]="valor">Texto</option>
</select>
changeSelect(e: any) {
    var indexSel = e.target.selectedIndex;
    var label = e.srcElement[indexSel].label;console.log(label);
}
lczapski
  • 4,026
  • 3
  • 16
  • 32
J.Mas
  • 11
  • 1
  • 3