Jessica
Jessica

Reputation: 63

Showing only the selected name

I am trying to show the selected text instead of the value because the value is Number. when you select from the option I am using Jquery to pass some data. I am only showing part of the table in which I am facing the issue.

<tr>    
<td>Category</td>   
    <td>
<select id="Category" name="size">
    <?php
        dbconnect(); 
        $stmt = $conn->prepare("SELECT CatID, Name FROM sizeWHERE ppid=:id");
        $stmt->bindParam('id',$id);
        $stmt->execute();
        $i = 0;
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 
        foreach ($rows as $row) {
            if ($i == 0) {
                echo '<option SELECTED value="'.$row['CatID'].'">'.$row['Name'].'</option>';
            }else {
                echo '<option value="'.$row['CatID'].'">'.$row['Name'].'</option>';
            }
            $i++;
        }
    ?>
</select>
</td>
</tr>

Jquery var cat = $('#Category').text(); $('#sSize').text(cat);

Summary

I want to show the selected option, I tried:

var cat = $('#Category').text();

but that showed all the options of the size.

$('#sSize').text(cat);

Please how can I do this

Upvotes: 2

Views: 68

Answers (2)

Abhitalks
Abhitalks

Reputation: 28417

To extract the selected value:

$('#Category').val();

To extract the text of selected option:

$('#Category :selected').text();

Upvotes: 0

Rooster
Rooster

Reputation: 10077

You need the selected option:

$('#Category option:selected').val();

which gives you the option val. You can get the text between the option and close tag like:

$('#Category option:selected').text();

Lastly, SELECTED doesn't make a select option selected. You need to do:

<option value="whatever" selected="selected">SOME TEXT</option>

Upvotes: 1

Related Questions