Obtain by JQuery the value of an element

0

How can I get the value that I have inside is input?

<p id="1">1 <input value="" type="text"></p>
    
asked by Eduardo 08.05.2018 в 14:46
source

2 answers

1

Try searching for the element with the id 1 that would be the element p and then the input :

function obtenerValor()
{
  console.log($("#1 input").val());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="1">1 <input value="" type="text"></p>
<button onclick='obtenerValor()'>Obtener valor</button>
    
answered by 08.05.2018 / 14:48
source
0

If it is for a field type of text or numeric, it would be done with .val()

If you want to get the text contained within a html element, it is done with a .text()

Like this:

function obtenerValorDeCampo()
{
  console.log($("#campo").val());
}

function obtenerValorDeElemento()
{
  console.log($("#1").text());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="1">1 <input id="campo" value="" type="text"></p>
<button onclick='obtenerValorDeCampo()'>Obtener valor de campo</button>
<button onclick='obtenerValorDeElemento()'>Obtener valor de elemento</button>
    
answered by 08.05.2018 в 15:11