Fetch the returned value via REST and insert into an input

Asked

Viewed 42 times

0

I’m searching, via Restful, the value of id of a user registered in an API. Now I have to enter this value in a input HTML. I’m trying this way:

I capture the value of the variable in a tag paragraph with class (value) and id (name)

and the value appears normally on the screen.

have the input where I want to enter this value which is;

<input class="campo" type="text" value="">

And there’s Javascript where I try to do this insertion which is;

let novoValor = document.querySelector(".valor").value; 
console.log(novoValor); 
let campo = document.querySelector(".campo");

campo.value = novoValor;

If I give a console.log(novoValor) appears in the browser console that:

<p data-brackets-id="1122" class="valor" id="id">6</p>

which is the exact id I’m currently searching for. Only in the input instead of the 6 value that is the user id, this message "Undefined"

Can someone help me?

  • Please, I need the answer in javascript, not frameworks

  • How is your API ?

  • Paragraph element, <p>, does not have the attribute value. If you want to get the contents of tag, use the attribute innerHTML

  • I am capturing the id and making a variable with it. If you read a little better you will see that I am inserting it into an input that has a value

  • I’m not interested in putting the value in the same place I’m looking for the same value

  • if the value is already in the <p> tag so I would need to put it there again?

  • @LR10 my api is online, it’s in java and I’m not the one who did it

  • With document.querySelector(".valor") you seek an element <p> in the DOM and tries to access the value of value of it. Elements <p> do not have the attribute value, so he shows up undefined. Search for the innerHTML who will understand what to do.

  • All right @Andersoncarloswoss, my input code is this <script> Let newValor = Document.querySelector(".value"). value; console.log(newValor); Let field = Document.querySelector(".field"); field.value = newValor; </script> where I place innerHTML. Like I don’t even know about frontend, that’s why I go to the forum

  • 1

    @Francisvagnerdalight, just change document.querySelector(".valor").value for document.querySelector(".valor").innerHTML

Show 5 more comments
No answers

Browser other questions tagged

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