Reputation: 12171
How do I use JQuery to get number from the drop down select?
<select aria-invalid="false" id="RatePercent" class="wpcf7-form-control wpcf7-select ratePercent" name="RatePercent">
<option value="">---</option>
<option value="Floating-6.5%">Floating-6.5%</option>
<option value="6 Months-5.65%">6 Months-5.65%</option>
<option value="1 Year-5.85%">1 Year-5.85%</option>
<option value="18 Months-5.99%">18 Months-5.99%</option>
<option value="2 Years-6.19%">2 Years-6.19%</option>
<option value="3 Years-6.85%">3 Years-6.85%</option>
<option value="4 Years-7.19%">4 Years-7.19%</option>
<option value="5 Years-7.40%">5 Years-7.40%</option>
</select>
If you choose 1 Year-5.85%, it returns '5.85', instead of '1 Year-5.85%'?
Upvotes: 1
Views: 70
Reputation: 5123
Use split here like:
$('#RatePercent').change(function() {
var val = $(this).val();
if (val != '') {
val = parseFloat(val.split('-')[1].split('%')[0]);
}
alert("Required value= "+val);
});
hope this will help.
Upvotes: 0
Reputation: 5361
My suggestion, use $.data()
<select aria-invalid="false" id="RatePercent" class="wpcf7-form-control wpcf7-select ratePercent" name="RatePercent">
<option value="">---</option>
<option value="Floating-6.5%" data-number="6.5">Floating-6.5%</option>
<option value="6 Months-5.65%" data-number="5.65">6 Months-5.65%</option>
</select>
to access:
$(this).data("number");
Upvotes: 0
Reputation: 173642
Using a regular expression:
$('#RatePercent').on('change', function() {
var re = /(\d(?:\.\d*))%$/,
matches = this.value.match(re);
if (matches) {
console.log(matches[1]);
}
});
Upvotes: 0
Reputation: 6612
Try this:
$('#RatePercent').change(function() {
var val = $(this).val();
if (val != '') {
val = parseFloat(val.split('-')[1]);
}
console.log(val);
});
Upvotes: 1
Reputation: 922
Use the .prop() and the replace function:
$(selector).prop("value").replace(/.*\-([0-9\.]+).*/, "$1");
This pulls the full value from the property and returns just the number.
Here is the jsfiddle: http://jsfiddle.net/zR6FC/
Upvotes: 1