Filling in input value by content from div

I am trying to grab the text inside a div, and then put it as value of the input type. so whatever is in the the div, upon button click, get inserted into value="" attribute. Not sure, what i should be getting, as in jquery the .val method would work. Here is my link, perhaps like this?

Capitalisation is important, so getElementByid should instead be getElementByid

Then, you need to give the div an id attribute. It currently has a class attribute instead.

Finally, you need to wrap the scripting code in a function, that is run when you click on the button. For you to attach that function to the onclick event of the button, you will want to give that button an identifier too.

After making some updates, and fixing a majority of poor coding practices, we end up with the following working example, that can be seen at https://jsfiddle.net/pmw57/9nyv1mo8/3/

<div id="divText">
This is value
</div>
<div>
   <input type="text" name="name" id="name" value="">
   <input type="submit" id="button" value="Lets do it">
</div>
document.getElementById("button").onclick = function () {
    var info = document.getElementById("divText").innerHTML;
    document.getElementById("name").value = info;
};

I am learning ir right, it should be getElementById instead of getElementByid, but thanks it work, just right.

Very important :wink: .

1 Like

Oh dear gawd, thank you iOS autocorrect!

I think that I fixed it :hammer: :grin:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.