1

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

3
  • 2
    Use .val() instead of .text() Commented Jan 23, 2021 at 8:39
  • gives undefined in console Commented Jan 23, 2021 at 8:41
  • 1
    Than don't use inline onclick. Remove that attribute and use a class like <button class="linkProduct" and in jQuery (...have you included the jQuery library at all?) use $(".linkProduct").on("click", linkproduct); Commented Jan 23, 2021 at 8:44

2 Answers 2

1

try this

var b = $(button).closest("td").find('select.productid').val();
Sign up to request clarification or add additional context in comments.

Comments

1

maybe you could try with something like this :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.