0

Hello everyone I am getting the value from dropdownlist using jquery here is my jquery code

var priceValue = $("#ddlprice option:selected").text(); 

and i am getting value Valvet ($100)

but i want only 100 from this value so how can I extract the exact value from it. Thanks

Gaurav_0093
  • 1,040
  • 6
  • 28
  • 56
  • Possible duplicate of [Regex using javascript to return just numbers](https://stackoverflow.com/questions/1183903/regex-using-javascript-to-return-just-numbers) – JYoThI Jun 27 '17 at 05:52

2 Answers2

1

Use regular expression to get the number

/\d+/g this will search for number in a given string 

var priceValue = "Valvet ($100)";


console.log(/\d+/g.exec(priceValue)[0]);
Dinesh undefined
  • 5,490
  • 2
  • 19
  • 40
0

if the value is 100 like this

<select id="ddlprice">
  <option value="100">Valvet ($100)</option>
</select>

then you get the value using val() like this $("#ddlprice").val(); to get the value

if your option is written like this <option>Valvet ($100)</option> or like this <option value="Valvet ($100)">Valvet ($100)</option> then you can use the function below from SO which will print only the numbers in a string.

function getNumbers(inputString){
    var regex=/\d+\.\d+|\.\d+|\d+/g, 
        results = [],
        n;

    while(n = regex.exec(inputString)) {
        results.push(parseFloat(n[0]));
    }

    return results;
}
var priceValue = $("#ddlprice option:selected").text(); 
console.log(getNumbers(priceValue));
Ram Segev
  • 2,563
  • 2
  • 12
  • 24