Hamza Qureshi
Hamza Qureshi

Reputation: 202

how to get selected td row value in javascript?

i want to select selected row value on button on click for this in select option i have name="product_id" and class="productid" i want value of product_id so i have this code

                <td>
                <select class="productid" name="product_id" >
                @foreach($findadminproducts as $a)
                <option value="{{ $a->id }}" >{{ $a->name }}</option>
                @endforeach
                </select>
                    <button onclick='linkproduct(this);'>Use Location</button>
                </td> 

method i tried in js

<script>
   function linkproduct(button){
     var b = $(this).closest("td").find('select.productid').text();
     console.log(b);
}
</script>

but it gives empty value or if i use .val() then it gives undefined

Upvotes: 1

Views: 115

Answers (2)

Guimby
Guimby

Reputation: 31

maybe you could try with something like this :

function linkproduct(button){
     var b = $(button).closest("td").find('select option:selected' ).text();
     console.log(b);
}

Upvotes: 1

Ostaad g
Ostaad g

Reputation: 301

try this

var b = $(button).closest("td").find('select.productid').val();

Upvotes: 1

Related Questions