Take a field value by id and put it into a variable

Asked

Viewed 1,515 times

4

Hello, I have a field on a form that receives the id: "teste", how can I catch via javascript this value and put in a variable??

I found the following on the internet

var str = document.getElementById("teste").value;

But it brings me value undefined, there is some other way?

My form:

<div class="divFormulario">
            @(Html.DevExtreme().Form<FormaPagamentoViewModel>
    ()
    .ID("formularioCadastro")
    .ShowValidationSummary(false)
    .Items(items =>
    {
    items.AddGroup()
    .Items(groupItems =>
    {
       groupItems.AddSimpleFor(m => m.Fpg_quantidade)
      .Editor(e => e.NumberBox().ID("teste").Width("70px").OnKeyPress("key_press").OnEnterKey("enter")); 
    })
    .FormData(Model)
    )

</div>

<script>
 function enter() {
     str = document.getElementById("teste").value;
  }
</script>

Inspected page:

inserir a descrição da imagem aqui

  • Post your HTML file

  • Post your HTML and what you tried with Javascript

  • I don’t know what DOM is, my html is there

  • In javascript I just want to put in a variable the value, that’s all

  • All right, that was it?

  • Sorry, I posted the wrong version, I used .value msm

  • It will inspect browser elements and see how the field’s HTML looks.

  • Ready, posted in photo because it is easier to understand

  • @Sam in answer of user139742 I show what I return when I do innerHTML, is easier to understand

Show 4 more comments

4 answers

2

You can try something like this:

HTML:

<form method="get">
    <input type="text" id="inputId" />
    <input type="submit" id="submitId" value="Enviar" />
</form>

Javascript:

// Função que mostra o valor do input num alert
function mostrarValor() {
    alert(document.getElementById("inputId").value);
}

// Evento que é executado toda vez que uma tecla for pressionada no input
document.getElementById("inputId").onkeypress = function(e) {
    if (e.keyCode == 13) {
        mostrarValor();
        e.preventDefault();
    }
}

// Evento que é executado ao clicar no botão de enviar
document.getElementById("submitId").onclick = function(e) {
    mostrarValor();
    e.preventDefault();
}
  • My problem is that by doing document.getElementById("inputId").value he returns me Undefined, maybe it’s something in my html

2


You are using DevExtreme, I’ve been doing some research and you’d better use it onValueChanged instead of onEnterKey, because whenever the value is changed the function is executed

https://js.devexpress.com/Documentation/ApiReference/UI_Widgets/dxAutocomplete/Configuration/#onValueChanged

Just you do:

<div class="divFormulario">
            @(Html.DevExtreme().Form<FormaPagamentoViewModel>
    ()
    .ID("formularioCadastro")
    .ShowValidationSummary(false)
    .Items(items =>
    {
    items.AddGroup()
    .Items(groupItems =>
    {
       groupItems.AddSimpleFor(m => m.Fpg_quantidade)
      .Editor(e => e.NumberBox().ID("teste").Width("70px").OnKeyPress("key_press").onValueChanged("enter")); 
    })
    .FormData(Model)
    )

</div>

<script>
 function enter(e) { //Tem que passar e como parâmetro 
     str = e.value; //Aqui pega o valor que foi digitado
  }
</script>

1

The problem is you’re trying to get the value of a div.

Divs has no values, only input, textarea and other elements that receive user input.

If you want to recover internal HTML from div, use

document.getElementById("teste").innerHTML;

Or if you want to recover only the text, without the possible tag declarations, use

document.getElementById("teste").innerText;
  • innerText get back to me "" and innerHTML returns everything, but shows me the correct value, how can I get the value ?

  • "<input type="hidden" value="15" name="Fpg_quantidade"><div class="dx-texteditor-container">... innerHTML returns this to me, and I wanted only the value field

0

You must assign the ID you want to pick up in some html string.

For example:

<p id="teste">Algum texto de exemplo</p>
  • 2

    I did that, missed posting, sorry

Browser other questions tagged

You are not signed in. Login or sign up in order to post.