Greasemonkey: access the value of a <INPUT> element?  
Author Message
Mara Guida





PostPosted: 2006-6-13 4:13:50 Top

javascript, 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.

 
Erwin Moller





PostPosted: 2006-6-13 16:28:00 Top

javascript >> Greasemonkey: access the value of a <INPUT> element? 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
 
Mara Guida





PostPosted: 2006-6-13 18:21:00 Top

javascript >> Greasemonkey: access the value of a <INPUT> element? 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.