javascript – How to get value from an input using jquery?

Question:

The following line:

<a id="asociar-1500" style='color: #004881' href="#" title='Asociar al Proveedor' class="btn btn-danger"><input type="hidden" value = "true"></>

It generates the following HTML:

<a id="aosciar" class="btn btn-danger" title="Asociar al Proveedor" href="#" style="color: #004881">
<input type="hidden" value="true">
</a>

I would like to know how to obtain the Value of the Input that is in Hidden , for that I am testing with this jquery function:

$('a[id^=asociar-]').click(function (e) {

e.preventDefault();

var inputs = (this).getElementsByTagName('input');

console.log(inputs[0]);
var asociado = inputs[0].attr('value');

console.log(asociado);

});

But I have not managed to enter the input.

Answer:

Don't use attr use val instead

var asociado = inputs[0].val();// es como un get

If you change var inputs = (this) .getElementsByTagName ('input'); by var inputs = $ ('input')

Explanation:

In this case var inputs = (this).getElementsByTagName inputs is not a jQuery object , therefore it does not have any associated jQuery method

On the other hand, when inputs =$('input') is invoked, a wrapper is added that converts inputs to a jQuery object and tests your object for all the available methods

Scroll to Top