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

M

Mara Guida

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.
 
E

Erwin Moller

Mara said:
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
 
M

Mara Guida

Erwin said:
Mara said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top