<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form >
Currect Address: <input id="demo" type="text" >
<button type="button" onclick="reuse_addr()">Addr</button>
</form>
<script>
function reuse_addr() {
document.getElementById("demo").innerHTML = "123 Main St";
}
</script>
</body>
</html>
Where’s the script?
forgot to use code tags
I typically work with Jquery
, but I assume you could achieve this with pure Javascript
.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form >
Currect Address: <input id="demo" type="text" >
<button type="button" onclick="reuse_addr()">Addr</button>
</form>
<script src="//code.jquery.com/jquery.min.js"></script>
<script>
function reuse_addr() {
$('#demo').val('123 Main St');
}
</script>
</body>
</html>
Thank-you.
What’s the raw javascript solution? I thought this was a simple tangent to what I was doing. Then I discovered I couldn’t figure it out in raw js.
You just need to replace innerHTML
with value
in the original snippet.
Thanks again! That’s 45 minutes I’ll never get back! Just had to know.
What’s the rule you used? I was certain it was innerHtml.
What do you mean by that?
How did you know to use value instead of innerHtml?
A quick Google
search with a quick demo test.
I googled the hell out of it, but no joy. Can you share a link or the search you used?
https://www.google.com/search?q=javascript+get+value+of+input
First Stack Overflow
article. I knew to use .value
because you were trying to replace the value of the input field. Usually, I use .val()
if I want to replace a value or get a value of an input field. So I put 2 and 2 together.
Excellent feedback! Great work! Thanks for all your time.
Niche
Here’s some good guidance on when to use .value and when to use .innerHtml
https://stackoverflow.com/questions/8823498/setting-innerhtml-vs-setting-value-with-javascript
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.