473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Greasemonkey: access the value of a <INPUT> element?

Hi,

After I've populated a page with some <INPUT ...> elements I'd like to
have access to whatever is written there.

The best I've done (which does not work) is:

================
// ==UserScript==
// @name AddInput
// @author me
// @description Add an input field and save it with
GM_setValue()
// @namespace http://127.0.0.1/
// @include http://www.wisenut.com/*
// ==/UserScript=

var text_Function = function(evt, elm) {
GM_log(elm.getAttribute('id') + ' ==> ' + elm.getAttribute('value'));
//GM_setValue(elm.getAttribute('id'), elm.getAttribute('value'));
};

var newdiv = document.createElement('div');
newdiv.setAttribute('style', 'background-color:#cccccc;padding:8px');
var lbl = document.createElement('label');
lbl.setAttribute('for', 'inputname');
lbl.innerHTML = 'Name: ';
newdiv.appendChild(lbl);
var elem = document.createElement('input');
elem.setAttribute('id', 'inputname');
elem.setAttribute('type', 'text');
elem.setAttribute('value', 'change me');
elem.addEventListener('blur', function(evt){text_Function(evt,elem);},
true);
newdiv.appendChild(elem);
document.body.appendChild(newdiv);
================

Is there any way to get the name written in the input field instead of
"change me"?

Thank you.

Jun 12 '06 #1
2 5254
Mara Guida wrote:
Hi,

After I've populated a page with some <INPUT ...> elements I'd like to
have access to whatever is written there.

The best I've done (which does not work) is:

================
// ==UserScript==
// @name AddInput
// @author me
// @description Add an input field and save it with
GM_setValue()
// @namespace http://127.0.0.1/
// @include http://www.wisenut.com/*
// ==/UserScript=

var text_Function = function(evt, elm) {
GM_log(elm.getAttribute('id') + ' ==> ' + elm.getAttribute('value'));
//GM_setValue(elm.getAttribute('id'), elm.getAttribute('value'));
};

var newdiv = document.createElement('div');
newdiv.setAttribute('style', 'background-color:#cccccc;padding:8px');
var lbl = document.createElement('label');
lbl.setAttribute('for', 'inputname');
lbl.innerHTML = 'Name: ';
newdiv.appendChild(lbl);
var elem = document.createElement('input');
elem.setAttribute('id', 'inputname');
elem.setAttribute('type', 'text');
elem.setAttribute('value', 'change me');
elem.addEventListener('blur', function(evt){text_Function(evt,elem);},
true);
newdiv.appendChild(elem);
document.body.appendChild(newdiv);
================

Is there any way to get the name written in the input field instead of
"change me"?

Thank you.


Hi,

What about giving the element a name?
Or get it by its ID?

So name it like:
elem.setAttribute('name', 'someName');
document.forms[0].someName.value = "something new";

or
document.getElementById('inputname').value = "something new";

I am unsure if I understand what your script is supposed to do, because as
far as I can see you could just write this part in HTML without
dom-interaction, but maybe my suggestions help.

Regards,
Erwin Moller
Jun 13 '06 #2
Erwin Moller wrote:
Mara Guida wrote:
Hi,

After I've populated a page with some <INPUT ...> elements I'd like to
have access to whatever is written there.

The best I've done (which does not work) is:

================
// ==UserScript==
// @name AddInput
// @author me
// @description Add an input field and save it with
GM_setValue()
// @namespace http://127.0.0.1/
// @include http://www.wisenut.com/*
// ==/UserScript=

var text_Function = function(evt, elm) {
GM_log(elm.getAttribute('id') + ' ==> ' + elm.getAttribute('value'));
//GM_setValue(elm.getAttribute('id'), elm.getAttribute('value'));
};

var newdiv = document.createElement('div');
newdiv.setAttribute('style', 'background-color:#cccccc;padding:8px');
var lbl = document.createElement('label');
lbl.setAttribute('for', 'inputname');
lbl.innerHTML = 'Name: ';
newdiv.appendChild(lbl);
var elem = document.createElement('input');
elem.setAttribute('id', 'inputname');
elem.setAttribute('type', 'text');
elem.setAttribute('value', 'change me');
elem.addEventListener('blur', function(evt){text_Function(evt,elem);},
true);
newdiv.appendChild(elem);
document.body.appendChild(newdiv);
================

Is there any way to get the name written in the input field instead of
"change me"?

Thank you.


Hi,

What about giving the element a name?
Or get it by its ID?

So name it like:
elem.setAttribute('name', 'someName');
document.forms[0].someName.value = "something new";

or
document.getElementById('inputname').value = "something new";

I am unsure if I understand what your script is supposed to do, because as
far as I can see you could just write this part in HTML without
dom-interaction, but maybe my suggestions help.

Regards,
Erwin Moller

LOL, sometimes the simplest things give too much work for ignorant
people.
I was only complicating the function every time it didn't work, instead
of simplifying.
Thank you very much.

After reading your reply I tried the simple

GM_log(evt.target.value);

and it worked :)

The purpose of the script is to allow configuration of the script
itself without having to open the "about:config" page.

Jun 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Don | last post by:
Using PHP, I'm creating an <input> tag. How do you delimit the text string for the value parm of an <input> tag, when the string contains the delimit char? Example: <input...value="This text...
3
by: chirs | last post by:
Hi, I want to put a value in a cookie. The following code does not work. It does not store the box1.value in the cookie. How can I fix it? <input type="text" name="box1"...
4
by: Martin Lucas-Smith | last post by:
I am wanting to know whether are XHTML1-valid characters for use within an id attribute and/or a name attribute. ...
2
by: not 2 swift | last post by:
I thought I would update an old page on which I had used a <INPUT TYPE=image ...> with a <BUTTON><IMG SRC=...></BUTTON> The problem is that <BUTTON> always provides a shadow/emboss effect. Can...
3
by: iinet | last post by:
How can i set in my css a min width for input elements, but leave the max size dynamic? Ben
3
by: TR | last post by:
Is it possible with CSS to prevent this wrapping alignment with a checkbox with a nested label? This is the label of the checkbox that wraps beneath it I'd prefer it looked like...
5
by: Bart van Deenen | last post by:
Hi all I have a form with a couple of input fields, embedded within spans. I am using script.aculo.us for dragging and dropping, and want to reorder the input fields that way. The input fields are...
2
by: Richard Maher | last post by:
Hi, I'm trying to use the Visibility Style attribute for a Div to effectively PopUp a lightweight window with some additional context-sensitive information, when a user mouses over a given...
1
by: JPhilS | last post by:
Hi to all Webmaster! I have discover this problem when I inserted the scores of the students i a centain subject. I am making a school project with regards to saving students' record. first, I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.