Home > database >  How to fill text field using JavaScript?
How to fill text field using JavaScript?

Time:09-16

I am trying to open a webpage from my JavaScript function:

function fillText() {   
      window.open("https://Somerandonwebsite.com/task");      
  }

The webpage : "https://Somerandonwebsite.com/task" has a text box with the following tag:

<input type="text" id="titleBox" autocomplete="off" aria-label="Title Field" title="" placeholder="Enter Title" aria-invalid="true">

I want to add string "Title created" to the text box with the id titleBox

How can I achieve this in the function? I am also confused whether to add the string first and open webpage or open webpage and access text box to fill my string

How can I access text box to fill this string into the text box on opening web page?

CodePudding user response:

As @Carsten Løvbo Andersen's comment said if you own both sites, you can just pass some query strings from your function to your site. E.G.

function fillText() {   
  window.open("https://Somerandonwebsite.com/task?string=yourString");
}      

Then, at your Somerandonwebsite.com, simply create a script:

var input = document.getElementById("titleBox");
const params = new URLSearchParams(window.location.search)
input.value = params.get("string")

This will make your text box display your query string named string

  • Related