Filimoni Naisua
Filimoni Naisua

Reputation: 41

How do i get value of inputbox

<td class="stockQuantity">
  <input class="form-control" id="Cart1" name="qty_req" required="required" type="text" value=""><br>
   <button type="button" onclick="appendCart(event,4,4,'Bottles',444,4)" class="btn btn-primary">
    Add To Cart
   </button>
</td>

Js function

 function appendCart(e, id, inwardRegId, stockName, quantity, inwardItemFId) {
      
        e = e || window.event;
        var target = e.target || e.srcElement;



        console.log(e);
        console.log(id);
        console.log(inwardRegId);
        console.log(stockName);
        console.log(quantity);
        console.log(inwardItemFId);

       // undefined 
        var inputVal = $(this).find('input[type="text"].stockQuantity').val();

        alert(inputVal);
}

How do I get the value in the input box above it using jQuery, keep getting undefined value when executed.

Note

Upvotes: 0

Views: 93

Answers (4)

Filimoni Naisua
Filimoni Naisua

Reputation: 41

Solved Using target.offsetParent.childNodes[0].value thank you everyone for your efforts

Upvotes: 1

Ali Waqas
Ali Waqas

Reputation: 335

Solution 1

if this is going to be the only input in the page then you can use the following snippet:

var inputVal = $('input[type="text"]')[0].value;
console.log('IV: ',inputVal)

or simply

$("input#Cart1").val();

Solution 2

for multiple input case

Now considering your code snippet, i see you are looking to find the input field using jquery methods.

for this to work, you must bind a click event after render

  $(document).on("click", "#button1", function () {
    var inputVal = $(this).siblings('input[id="Cart1"]')[0].value
    alert(inputVal);
  })

similarly your HTML will be like this

<button id="button1" type="button" class="btn btn-primary">
          Add To Cart
</button>

Let me know if you need any more help, thanks

Upvotes: 1

IAMSTR
IAMSTR

Reputation: 684

Problems

1. The $(this) placement is wrong, basically what you are saying is find the input in the button, which it cannot find , the context is wrong that's why you cant find the value of the input field

2. There is no input with a classname .stockQuantity

Solutions

  1. $("input#Cart1").val();
  2. $("body").find('input[type="text"]').val()

Upvotes: 1

spadletskys
spadletskys

Reputation: 125

Firstly, the input box of yours has no class called stockQuantity. It is the td element that has it.

What you can do is this:

 function appendCart(e, id, inwardRegId, stockName, quantity, inwardItemFId) {
      
        e = e || window.event;
        var target = e.target || e.srcElement;



        console.log(e);
        console.log(id);
        console.log(inwardRegId);
        console.log(stockName);
        console.log(quantity);
        console.log(inwardItemFId);

       // undefined 
        var inputVal = $("input#Cart1").val();

        alert(inputVal);
}
<td class="stockQuantity">
  <input class="form-control" id="Cart1" name="qty_req" required="required" type="text" value=""><br>
   <button type="button" onclick="appendCart(event,4,4,'Bottles',444,4)" class="btn btn-primary">
    Add To Cart
   </button>
</td>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Tell me if this worked on you!

Upvotes: 1

Related Questions