javascripthtmlcssweb-deploymentstatic-site

How To Fill-Up a particular field of HTML form Based On The Button Clicked


I am making a Static Website with 5 kind of different home aomation service.

These are having different prices.

Whenever they click on "book now" button, A Booking form pops up.

Now, I want to fill up the "service required" field in the form automatically.

For Example if someone's clicking the book now button for "Full Home Automation Package" the "sevice required" will automatically be filled with that name.

How can i do so using html and JavaScript


Solution

  • You just need to give your button a function where you get your

    text content and paste it to your Input, like this:

    function MyFunction(){
    	var value = document.getElementById("Test").textContent; 
      document.getElementById("test2").value=value;
    }
    <p id="Test"> Is this what you want ? </p>
    <button type="button" onclick="MyFunction()">Click</button><br><br>
    <input type = "text" id="test2" name "teste2">