By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,570 Members | 1,613 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,570 IT Pros & Developers. It's quick & easy.

Problem with setattributevalue and Firefox 2

P: n/a
Hi everybody!
This is the first post in which I'll stress somebody in this NG :-).. U
look as experts, and always kindly available, so I'm here begging for
your help! :-) (pardon me if somebody already asked for this):
I'm studying Javascript, DOM, Ajax & company to make a "site" for an
exam at university... I've read a guide on HTML.it (an italian "guide
site"), and there is an example which doesn't work in Firefox 2;
in this page,
http://javascript.html.it/guide/lezi...gli-attributi/
about in the middle there is an example with a textarea, with three
buttons. on the first, in the onclick event it "tries" to give value
attibute the "inseriamo del testo" value ("inseriamo del testo" means
"let's insert some text"), so the text should appear in the textbox...
(there is also the source in a yellow box in the page) but it doesn't
work! It works in IE, but not in Firefox! Why? I can't believe it's
incompatible!!
Thanks in advance!
Massi
Dec 1 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ASM
massic80 a écrit :
in this page,
http://javascript.html.it/guide/lezi...gli-attributi/
onclick event "tries" to give value
attibute the "inseriamo del testo" value
It works in IE, but not in Firefox! Why? I can't believe it's
incompatible!!
I think it is incompatible because it is ;-)

Or does FF thinks with a textarea, or an input text, value can't be a
settable attribute because it is reserved to user or at least clearly
modified by assigning directly something to the value ?
(document.form.field.value = 'something')

onclick="document.getElementsByTagName('textarea') .item(0).setAttribute('value',
'inseriamo del testo')"

Can we really consider that "value" for a textarea is an "attribute" ?

In same way you'll can't set value of a text field by attribute in FireFox.

Try :

onclick="
document.getElementsByTagName('textarea')[0].value='inseriamo del testo';"

And always prefer use of document's form tree :

onclick="this.form[0].value = 'inseriamo del testo';"
(first element of form containing button with this onclick)

Remember you'll have same problem with input text, and probably input
hidden, password, and perhaps button, reset, submit.

Test :
======

<html>
<form onsubmit="return false">
<input >
<input type=button onclick="this.form[0].value = 'inseriamo del testo';"
value="Assegna il testo">
<input type=button onclick="this.form[0].value = '';"
value="Vuoto il testo">
<input type=button value="Attribuisci del testo"
onclick="var t = document.setElementsByTagName('textarea')[0];
t.setAttribute('value','Attribuiamo del testo';">
</form>
</html>

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 1 '06 #2

P: n/a
ASM
ASM a écrit :
>
Test :
======
Test :
======

<html>
<form onsubmit="return false">
<input >
<input type=button onclick="this.form[0].value = 'inseriamo del testo';"
value="Assegna il testo">
<input type=button onclick="this.form[0].value = '';"
value="Vuoto il testo">
<input type=button value="Attribuisci del testo"
onclick="var t = document.getElementsByTagName('textarea')[0];
t.setAttribute('value','Attribuiamo del testo';">
</form>
</html>

Anyway : doesn't work better.

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 1 '06 #3

P: n/a
ASM ha scritto:
I think it is incompatible because it is ;-)
Thanks ASM (x86? ;-))! (do U know italian? there R some italian
sentences I didn't write! :-))
onclick="document.getElementsByTagName('textarea') .item(0).setAttribute('value',

'inseriamo del testo')"

Can we really consider that "value" for a textarea is an "attribute" ?
I dunno, I'm still studying it... and it even suggested to use that
grammarm instead of [0]... it said it was more clear.. (BTW, I would
have started using the other (similar to C++ and Java)... but when the
EXAMPLES don't work.....! :-D)
onclick="
document.getElementsByTagName('textarea')[0].value='inseriamo del testo';"
IT WORKS! :-D
onclick="this.form[0].value = 'inseriamo del testo';"
works too! :-) OK, I'll never use that function anymore (and, I repeat,
I wouldn't have used it if not in that example)! :-)
Remember you'll have same problem with input text, and probably input
hidden, password, and perhaps button, reset, submit.
....so... never guess attributes, just use the other notation? ;-)
<html>
<form onsubmit="return false">
<input >
<input type=button onclick="this.form[0].value = 'inseriamo del testo';"
value="Assegna il testo">
<input type=button onclick="this.form[0].value = '';"
value="Vuoto il testo">
<input type=button value="Attribuisci del testo"
onclick="var t = document.setElementsByTagName('textarea')[0];
t.setAttribute('value','Attribuiamo del testo';">
</form>
</html>
Yes... doesn't work even this way....
Can I put two or more instructions in the same string in an event like
that (and like U did)?
Another small question: why, sometimes, I see in certain examples...
<br/instead of <br>?
Thanks again! ;-)
Massi
Dec 2 '06 #4

P: n/a
ASM
massic80 a écrit :
ASM ha scritto:
(do U know italian?
Ma chè si ! multo bene : gelati, machina, hotel, pizza, buongiorno.
Pericolo sporgersi
e poi? basta ... ;-)
>Remember you'll have same problem with input text, and probably input
hidden, password, and perhaps button, reset, submit.

...so... never guess attributes, just use the other notation? ;-)
Usually when I play with elements of forms I use the old fashion JS
document.forms[x].elements[y].
name, id, type, value, checked, onclick and other events
but with FF it seems that, except for value, we can use DOM JS
and its set/get Attribute()
Yes... doesn't work even this way....
of course there was an error :-(
Can I put two or more instructions in the same string in an event like
that (and like U did)?
<html>
<form onsubmit="return false">
<input type=text value="">
<input type=button value="Assegna il testo e onclick"
onclick="
this.form[0].value = 'inseriamo del testo';
this.form[0].onclick = function() {
alert('my value is : '+this.value); };"
>
<input type=button value="Attribuisci onchange"
onclick="
var T=document.getElementsByTagName('INPUT')[0];
T.setAttribute('onchange',
'alert(\'esta ha value : \'+this.value)');"
>
<input type=button value="vedere gli attributi"
onclick="
var T=document.getElementsByTagName('INPUT')[0];
alert('onchange = '+T.getAttribute('onchange') +
'\nonclick = '+T.getAttribute('onclick') +
'\nJS value = '+T.value +
'\nDOM value = '+T.getAttribute('value'));"
>
<input type=button value="Riverse"
onclick="
var T=document.getElementsByTagName('INPUT')[0];
T.setAttribute('onchange','');
T.value = null;
T.onclick='';"
>
</form>
</html>

Another small question: why, sometimes, I see in certain examples...
<br/instead of <br>?
You'll have to learn some HTML documentation ;-)
In strict or in xhtml you can't have tag not closed
so with tags as meta, link, hr, br ... you use auto-closer tags <tag />
In transitional you use <hr(but <hr /is allowed)

http://www.w3.org/QA/2002/04/valid-dtd-list.html
http://www.w3.org/TR/html401/strict.dtd
http://www.w3.org/TR/html401/
http://www.gdesign.it/pages/howto/ar...pe/doctype.php
http://www.gdesign.it/pages/principi...li/browser.php

gdesign can also tell wrong things :
http://www.gdesign.it/pages/html/image/imgtag.php
(about attribute alt)

If you use FireFox I recommend these extensions :
http://users.skynet.be/mgueury/mozilla/
http://chrispederick.com/work/webdeveloper/
http://forum.softwareblaze.com/viewtopic.php?t=2
http://www.mozilla.org/projects/inspector/

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 2 '06 #5

P: n/a
ASM ha scritto:
>(do U know italian?
Ma chè si ! multo bene : gelati, machina, hotel, pizza, buongiorno.
Pericolo sporgersi
e poi? basta ... ;-)
OK, I'll correct U, now, in change of your help: mOlto, maCChina, che
without accent, and "ma che sì, doesn't mean much! :-P
BTW, nice try! ;-)
Usually when I play with elements of forms I use the old fashion JS
document.forms[x].elements[y].
Yeah, thanks! Being that I need to Use script.aculo.us, I'll use the $
function, and .value :-)
>Can I put two or more instructions in the same string in an event like

<html>
(...)
</html>
Didn't work much... but may not be for an eventual answer to my
question, so I'll try, thanks again ;-)
>Another small question: why, sometimes, I see in certain examples...
<br/instead of <br>?

You'll have to learn some HTML documentation ;-)
Thanks for the advice & links! :-)
Massi
Dec 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.