Michael Ortega
Michael Ortega

Reputation: 60

Changing Text Input from Chrome Bookmark

I am trying to fill out text input's on a site that does not have JQuery. However, I keep getting a null return when trying to use Javascript's getElementById function with a Google Chrome Bookmark.

How to replicate:

Go to W3Schools: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_text_value2

Add this as a bookmark:

javascript:(function(){
    console.log(document.getElementById("myText"));
})();

Click on the bookmark and see "null" in the console instead of the text input.

(To get the desired output you can change W3's code to console.log the element instead of changing it's value and then click the W3's "Try it" button)

--

The goal here is to be able to change these text input values by script.

Ex) Change "Mickey" to "Johnny Bravo" through bookmark click

Upvotes: 0

Views: 1528

Answers (1)

Rich Moss
Rich Moss

Reputation: 2384

Drag the padlock icon to your bookmarks toolbar, right-click, edit and set the URL to:

javascript:document.getElementById("myText").value = "Johnny Bravo";document.close();

Then click it to change the value of myText. You need to close the document after setting the value or it will write to a new document.

The bookmarklet above won't work in your TryIt editor because the form is in an iFrame named 'iframeResult'. But this works:

javascript: window.frames.iframeResult.document.getElementById("myText").value = "Johnny Bravo";document.close();

Upvotes: 1

Related Questions