Sires
Sires

Reputation: 59

Html5 Dropdown get text reults as NaN

I got a dropdown Menu which is done in php and looks like this:

function HowDangerIsAProduct(){
        $howDangerIsAProductInNumbers = array("secure","mostly Secure","very Danger");

        $stringchain = '<select class="form-control" id="HowDangerIsaProduct" name="HDIAP">';
        $stringchain .= '<option disabled="disabled">How Danger would you rate that product</option>';



        foreach ($howDangerIsAProductInNumbers as $hdiapin)
        {
            $stringchain .= '<option>'
                 .$hdiapin
                 .'</option>';
        }

        $stringchain .= '</select>';

        return $strinchain;
    }

Now the user should choose one option and I want to get the text, so i build up some javascript/jQuery code to fetch the selected text:

$('#HowDangerIsaProduct').change(function(){
    Impact =  + $(this).text();
});

I also tried .val() but its result is always NaN But if I put numbers in the array (e.g. 1,2,3,4)it works fine but I want to save the text to a database.

Got any idea on how can I solve this problem?

Upvotes: 0

Views: 151

Answers (1)

Iceman
Iceman

Reputation: 6145

Impact =  + $(this).text();

will return Nan because you are trying make a string integer positive.

Therefore simply remove + before the string and make it:

Impact =  $(this).text();

Note: if text was a number use parseInt to a Number first.

Impact =  + parseInt($(this).text())

Upvotes: 2

Related Questions