1

I have input :

<input type="text"  id="nameProduct">

I want set value :

document.getElementById("nameProduct").value="hello";

How can I do it before page load input ?

1 Answer 1

2

You can't do it with JavaScript, the dom element object can only get after it loaded. The right way is to set value attribute for input.

<input type="text" id="nameProduct" value="hello">


A simple hack you can do is hide element initially and show it after value updated using JavaScript.

var ele=document.getElementById("nameProduct");
ele.value="hello";
ele.style.display='block';
<input type="text" id="nameProduct" style="display:none">

Sign up to request clarification or add additional context in comments.

2 Comments

No, I must do it in JS
I get a variable from other page and when I load this page I must set a variable in my input with js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.