Adding a button inside a text input field in a website form with the help of html, css, and javascript

I am interested in creating a greasemonkey script that can add a button inside text entry forms on various websites to enhance their appearance. I envision the text entry forms looking like this:

         ____________________ 
Username:|___________|BUTTON|
         ____________________
Password:|___________|BUTTON|

My goal is to achieve this functionality using javascript, html, and css. However, I need the script to be able to work on websites with unknown layouts, ruling out the image repositioning method described in this post: How do I add a "search" button in a text input field?

Answer №1

Let's kick things off with jQuery for a more concise approach.

$('form').each(function () {
    $(this).find('input').filter(':text').each(function () {
        $(this).after($('<button>').text('My Button'));
    });
});

Check out the demo here.

Edit: Now, let's dive into the non-jQuery version of the solution.

Alright, let's get started!

var forms = document.getElementsByTagName('form');
for (var i = 0; i < forms.length; i++) {
    var form = forms[i];
    var inputs = form.querySelectorAll("input[type=text]");
    for (var j = 0; j < inputs.length; j++) {
        var input = inputs[j];
        var button = document.createElement('button');
        button.innerText = 'My Button';
        input.parentNode.insertBefore(button, input.nextSibling);        
    }
}

You can view the updated code here.

I hope this explanation clears things up for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

BeautifulSoup is throwing an AttributeError because it cannot find the attribute 'h3' in the list object

I am a newcomer to the world of Web-scraping and I am currently working my way through this tutorial ( ) in order to collect movie data. However, I believe I have made a mistake in defining the variable "first_movie". This is the code I have: from reque ...

using response.redirect to navigate to a new page with a database value

<body> <%@page import="java.sql.*" %> <%@page import="javax.sql.*" %> <% String user=request.getParameter("username"); session.putValue("uname",user); String pass=request.getParameter("pass"); ...

Exploring Ways to Retrieve Property Names in AngularJS JSON

I need help finding a way to use the property name as the table header in my code example below: <table> <th ng-repeat="auditorium in auditoriums"> {{ auditorium.NAME }} </th> <tbody ...

Transmit updated value to masterpage upon selection alteration

In my current project using ASP.NET, I have a MasterPage that includes a navigation bar with different options. One of the options leads to another page where the company now requires me to pass a parameter through the link. After some research, I managed ...

Invoke a JavaScript file within the MongoDB shell

Running a JS file directly from the Mongo shell using the query mongo localhost:27017/west createSumCollection.js has resulted in an error: "uncaught exception: SyntaxError: unexpected token: identifier : @(shell):1:6" The content of CreateSu ...

Struggling to locate the module 'firebase-admin/app' - Tips for resolving this issue?

While working with Typescript and firebase-admin for firebase cloud functions, I encountered the error message "Cannot find module 'firebase-admin/app'" when compiling the code with TS. Tried solutions: Reinstalling Dependency Deleting node_modu ...

Colorful radial spinner bar

I am interested in replicating the effect seen in this video: My goal is to create a spinner with text in the center that changes color, and when the color bar reaches 100%, trigger a specific event. I believe using a plugin would make this task simpler, ...

A simple guide on integrating personalized color palettes into Material-UI Theme

One enhancement I'd like to make in my theme is the inclusion of a custom color called warn import React from 'react' import { MuiThemeProvider } from '@material-ui/core/styles' import createMuiTheme from '@material-ui/core/s ...

What is the best way to extract the image tag from HTML code and use it as the image source in Glide for images

I recently came across a method to extract image links from HTML img tags using the Html Java library. It requires using the fromHtml method. After pulling some HTML code from an RSS feed, I wanted to display the images it contained. The code snippet belo ...

Update the page using Ajax without relying on jQuery

I want to implement automatic page updates without the need for a refresh using Ajax. However, most resources I've come across suggest using jQuery and PHP, which are areas of coding I am not very familiar with. My goal is to integrate Ajax into my HT ...

The message I'm attempting to include in the request is not being transmitted along with the request

Currently, I am facing an issue while using Thunder Client to send requests with a POST method. Despite including the body contents and setting the content-type to application/json in the header, whenever I try to access req.body in the request section, ...

Using the HtmlTable Control to populate data from an HtmlTable object in ASP/C# Programming

It seems like a simple question, but I'm struggling to populate an HtmlTable control, accessible from codebehind, with data from an HtmlTable object already containing information. Could someone offer me guidance on this issue? Thank you in advance. ...

What is the best way to adjust the height of a Div to be 100%?

I've been struggling to make the .footer and the div .content have a height of 100%. While I know how to get the footer to stick at the bottom, I can't seem to make the content div reach the bottom of the page. Increasing the min-height on the co ...

Can you provide guidance on showcasing mongodb data in a flask template with flask_pymongo?

Apologies if my question seems a bit unclear. I am struggling to display data from MongoDB in a Flask template. The code I have currently isn't working as expected. Here's what I've attempted so far: (I tried to get creative) @app.route(&apo ...

Looking for a Horizontal Layout?

@foreach (var item in APModel) { <div class="row row-cols-1 row-cols-md-2 g-4"> <div class="col"> <div class="card" style="width: 18rem;"> <img src="h ...

Is there a way to retrieve values from a different menu without the need to refresh the page?

Whenever a radio button is clicked on a form, a popup menu is displayed. The popup menu is implemented using the Bootstrap module. However, there is an issue where selecting an option from the popup menu causes the form to reset, resulting in the loss of ...

Django: Occasionally, text entered into a text field may not be successfully submitted after a post request

Encountering a strange bug that is difficult to replicate consistently. In my HTML file, I use jQuery to populate a text field with Summernote. However, when I populate the text field, do not make any changes, and submit the form, the outcome is inconsiste ...

Tips for implementing a nested ng-repeat with nested JSON array data triggered by a button click

Assuming I have assigned the following JSON data to $scope.people: [ { "personId": 1, "name": "Thomas", "age": 39, "friends": [ { "friendId": 1, "nickName": "Lefty" ...

Transferring information between Flask and JS using AJAX for a Chrome extension

I'm experimenting with AJAX calls to establish communication between my Javascript frontend in a chrome extension and the Flask API where I intend to utilize my Machine Learning algorithms. content.js console.log("Let's get this application ...

What could be the reason for the sudden lack of content from the Blogger API?

For weeks, I've been using the Google API to retrieve JSON data from my Blogger account and showcase and style blog posts on my personal website. Everything was functioning flawlessly until yesterday when, out of the blue, the content section stopped ...