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
– Francis Vagner da Luz
How is your API ?
– LR10
Paragraph element,
<p>
, does not have the attributevalue
. If you want to get the contents of tag, use the attributeinnerHTML
– Woss
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
– Francis Vagner da Luz
I’m not interested in putting the value in the same place I’m looking for the same value
– Francis Vagner da Luz
if the value is already in the <p> tag so I would need to put it there again?
– Francis Vagner da Luz
@LR10 my api is online, it’s in java and I’m not the one who did it
– Francis Vagner da Luz
With
document.querySelector(".valor")
you seek an element<p>
in the DOM and tries to access the value ofvalue
of it. Elements<p>
do not have the attributevalue
, so he shows upundefined
. Search for theinnerHTML
who will understand what to do.– Woss
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
– Francis Vagner da Luz
@Francisvagnerdalight, just change
document.querySelector(".valor").value
fordocument.querySelector(".valor").innerHTML
– NoobSaibot