I'd like to know how I can access certain attributes of a <textarea> object; in specific i'd like to know if it's possible to somehow set the WRAP property (textarea.wrap doesn't work opposed to things like textarea.rows which does work). More in general, is it possible to give objects names or id's through JS?
Thanks
element.getAttribute('hi');
element.setAttribute('hi', '20');
element.id = 'hi';
element.name = 'hi';
element.className = 'hi';
-[Unknown]
thanks! but what about the WRAP attribute for textarea's? i'm very much hopig i can control it, but i very much fear that it isn't possible....
Why not...?
textarea.setAttribute('wrap', 'somevalue');
-[Unknown]
hadn't thought of that, I'll try it ASAP! thanks !!!
IT WORKS! you're my hero! :)
next question! i've used the setAttribute() like this:
textarea.setAttribute('onkeydown', 'someFunction(textarea.someAttribute);');
firefox likes it, but unfortunately iexplorer doesn't! is this is case of poor JS support in iexplorer or am i doing something horribly wrong here?
In that case, you want:
textarea.onkeydown = function () {someFunction(this.someAttribute);};
-[Unknown]
thanks again! never seen that syntax before, but it works great! :)
This is why I don't learn programming from books - if I did, I wouldn't know about things most people don't know about.
-[Unknown]
that's why i'm never afraid to ask and learn; even though i do learn from books as well. :)
I'm trying to add a function to a number of certain events. Instead of writing the some code over and over again for each event, i figured: let's be efficient and do it in a loop. For some reason it isn't working... I tried fixing it with an eval() on the document.events[ x ], but haven't found a way yet. Any help?
var events = new Array('onkeydown', 'onkeyup', 'onkeypress', 'onmousedown', 'onmouseup', 'onmouseclick');
for (var x = 0; x < events.length; x++) {
document.events[x] = function() {
// do stuff
}
}
var funky = function ()
{
alert('hi!');
}
document.onkeydown = funky;
document.onkeyup = funky;
Or you could use a defined (not anonymous) function.
-[Unknown]
i was kinda hoping to avoid using:
document.onkeydown = funky;
document.onkeyup = funky;
document.onkeypress = funky;
document.onmousedown = funky;
document.onmouseup = funky;
document.onmouseclick = funky;
[.....]
is there some kind off document.onevent thingy maybe? or is there a way to get this to work:
var events = new Array('onkeydown', 'onkeyup', 'onkeypress', 'onmousedown', 'onmouseup', 'onmouseclick');
for (var x = 0; x < events.length; x++) {
document.events[x] = myFunction(argument); // this line doesn't work !!!
}
i figured it out! thank you so much for your help! i did it like this:
var events = new Array('onkeydown', 'onkeyup', 'onkeypress', 'onmousedown', 'onmouseup', 'onmouseclick');
for (var x = 0; x < events.length; x++) {
eval('document.' + events[x] + ' = function() { myFunction(argument); }');
}
Quote from: Curbow 5 on September 17, 2004, 05:11:57 PM
i figured it out! thank you so much for your help! i did it like this:
var events = new Array('onkeydown', 'onkeyup', 'onkeypress', 'onmousedown', 'onmouseup', 'onmouseclick');
for (var x = 0; x < events.length; x++) {
eval('document.' + events[x] + ' = function() { myFunction(argument); }');
}
Sorry, my meaning was this:
var events = ['onkeydown', 'onkeyup', 'onkeypress', 'onmousedown', 'onmouseup', 'onmouseclick'];
var eventHandler = function() {myFunction(argument);};
for (var x = 0; x < events.length; x++)
document[events[x]] = eventHandler;
This is cleaner, because it:
- only defines the function once, eating less memory and making it easier to manage on change.
- it doesn't use eval, which is a potentially dangerous thing to use - security wise.
-[Unknown]
it doesn't work for me in FireFox... the code i gave was simplified, in reality i'm accessing an iframe's onkeyup, onkeydown etc. I'm using this:
var iframe = document.getElementById('myIframe').contentWindow;
var events = ['onkeydown', 'onkeyup', 'onkeypress', 'onmousedown', 'onmouseup', 'onmouseclick'];
var eventHandler = function() {myFunction(argument);};
for (var x = 0; x < events.length; x++)
iframe.document[events[x]] = eventHandler; // watch this line !!!
i need it to work both in IE (which it does) and Gecko!
So, wait, you're saying it works in IE but not Firefox?
What if you use the anonymous function for each, but without the eval?
-[Unknown]
Same thing: works in IE, but not in FireFox (1.0 PR). BTW, is using var events = ['item1', 'item2', 'and so on...'] basically the same as using new Array like I did? I now have the following three options which all work in IE, but not in FireFox.
The weird thing is, that in options 2 and 3 I must declare the iframe variable right before the rest of the code (even though it is defined exactly the same way earlier in the script) or receive a 'Permission denied' error in IE...
// option #1: anonymous function and eval on each
var events = new Array('onkeydown', 'onkeyup', 'onkeypress', 'onmousedown', 'onmouseup', 'onmouseclick');
for (var x = 0; x < events.length; x++) {
eval('iframe.document.' + events[x] + ' = function() { myFunction(argument); }');
}
// option #2: neither anonymous function nor eval on each
var iframe = fetchById('myIframe').contentWindow;
var events = ['onkeydown', 'onkeyup', 'onkeypress', 'onmousedown', 'onmouseup', 'onmouseclick'];
var eventHandler = function() { myFunction(argument); };
for (var x = 0; x < events.length; x++) {
iframe.document[events[x]] = eventHandler;
}
// option #3: anonymous function, but not eval on each
var iframe = fetchById('myIframe').contentWindow;
var events = new Array('onkeydown', 'onkeyup', 'onkeypress', 'onmousedown', 'onmouseup', 'onmouseclick');
for (var x = 0; x < events.length; x++) {
iframe.document[events[x]] = function() { myFunction(argument); };
}
It must be a compatiblity thing in Firefox then; I assumed it would work.
-[Unknown]
It's still not fixed, but apparantly (from what I've googled up) IE and Mozilla access 'Iframed' documents differently. Haven't validated this entirely yet. Does anyone have any experience with this:
// IE method:
var iframe = document.getElementById('htmleditor').contentWindow.document;
// Mozilla method:
var iframe = document.getElementById('htmleditor').contentDocument;