V4n1ll4
V4n1ll4

Reputation: 6099

Jquery get text of selected option dropdown

I have a HTML form and I am looping over some numeric input field values to out a total at the bottom of the page.

I need to get the text value (not the option value) of the nearest dropdown menu.

My HTML is as follows:

<div class="standard-row wsheet-row input-row" id="5438">
    <label class="ib">
        <span class="label-stacked">Category</span>
        <select class="inp inp220 inp-disabled" readonly="" data-myname="worksheet[*][plots][!][sub_category][]" name="sub_category">
            <option value="5438">NON LABOUR</option>
        </select>
    </label>
    <label class="ib">
        <span class="label-stacked">Total</span>
        <input class="inp inp110 data-addup-total inp-calculate" data-myname="worksheet[*][plots][!][total_cost][]" name="total_cost" value="0.00" type="text">
    </label>
</div>
<div class="standard-row wsheet-row input-row" id="5240">
    <label class="ib">
        <span class="label-stacked">Category</span>
        <select class="inp inp220 inp-disabled" readonly="" data-myname="worksheet[*][plots][!][sub_category][]" name="sub_category">
            <option value="5240">1ST FIX CYLINDER</option>
        </select>
    </label>
    <label class="ib"> 
        <span class="label-stacked">Price</span>
        <input class="inp inp110 inp-disabled" readonly="" data-myname="worksheet[*][plots][!][price][]" name="price" value="£40.00" type="text">
    </label>
    <label class="ib"> 
        <span class="label-stacked">Number</span>
        <input class="inp inp55 inp-calculate" data-myname="worksheet[*][plots][!][number_units][]" name="number_units" value="" type="text">
    </label>
    <label class="ib">
        <span class="label-stacked">Total</span>
        <input class="inp inp110 inp-disabled data-addup-total" readonly="" data-myname="worksheet[*][plots][!][total_cost][]" name="total_cost" value="£0.00" type="text">
    </label>
</div>
<div class="wsheet-labour-total">LABOUR: £4.00</div>
<div class="wsheet-nonlabour-total">NON LABOUR: £4.00</div>
<div class="wsheet-final-total">TOTAL CLAIM: £4.00</div>

The Jquery I am using to try and get the select before it is:

$(parentRow).find('.data-addup-total').each(function() {
    // get closest sub_category
    var subCategory = $(this).closest('.inp220 option:selected').prev().text();
    alert(subCategory);

    price = $(this).val().replace(/[^\d.-]/g, '');
    numtotal += parseFloat(price);
});

If I do console, I get:

Object { length: 0, prevObject: Object, context: <input.inp.inp110.data-addup-total.inp-calculate> } worksheet.js:158:1
Object { length: 0, prevObject: Object, context: <input.inp.inp110.inp-disabled.data-addup-total> } worksheet.js:158:1
Object { length: 0, prevObject: Object, context: <input.inp.inp110.data-addup-total.inp-calculate> } worksheet.js:158:1
Object { length: 0, prevObject: Object, context: <input.inp.inp110.inp-disabled.data-addup-total> } worksheet.js:158:1

Upvotes: 0

Views: 176

Answers (1)

guradio
guradio

Reputation: 15555

DEMO

$('.data-addup-total').each(function () {
    // get closest sub_category
    var subCategory = $(this).parent().siblings().find('.inp220 option:selected').text();
    alert(subCategory);
});

Try this

Upvotes: 1

Related Questions