Simple Machines Community Forum

General Community => Scripting Help => Topic started by: Curbow 5 on September 06, 2004, 12:33:17 PM

Title: JS accessing attributes
Post by: Curbow 5 on September 06, 2004, 12:33:17 PM
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
Title: Re: JS accessing attributes
Post by: [Unknown] on September 06, 2004, 01:19:11 PM
element.getAttribute('hi');
element.setAttribute('hi', '20');

element.id = 'hi';
element.name = 'hi';
element.className = 'hi';

-[Unknown]
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 06, 2004, 04:51:57 PM
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....
Title: Re: JS accessing attributes
Post by: [Unknown] on September 06, 2004, 05:01:57 PM
Why not...?

textarea.setAttribute('wrap', 'somevalue');

-[Unknown]
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 06, 2004, 05:07:29 PM
hadn't thought of that, I'll try it ASAP! thanks !!!
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 06, 2004, 05:26:29 PM
IT WORKS! you're my hero! :)
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 07, 2004, 02:42:09 AM
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?
Title: Re: JS accessing attributes
Post by: [Unknown] on September 07, 2004, 02:49:57 AM
In that case, you want:

textarea.onkeydown = function () {someFunction(this.someAttribute);};

-[Unknown]
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 07, 2004, 03:07:14 AM
thanks again! never seen that syntax before, but it works great! :)
Title: Re: JS accessing attributes
Post by: [Unknown] on September 07, 2004, 03:32:42 AM
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]
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 07, 2004, 05:18:35 AM
that's why i'm never afraid to ask and learn; even though i do learn from books as well. :)
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 17, 2004, 01:51:48 PM
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
}
}

Title: Re: JS accessing attributes
Post by: [Unknown] on September 17, 2004, 02:02:34 PM
var funky = function ()
{
   alert('hi!');
}

document.onkeydown = funky;
document.onkeyup = funky;

Or you could use a defined (not anonymous) function.

-[Unknown]
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 17, 2004, 04:22:19 PM
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 !!!
}
Title: Re: JS accessing attributes
Post by: 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); }');
}
Title: Re: JS accessing attributes
Post by: [Unknown] on September 17, 2004, 06:15:43 PM
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]
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 18, 2004, 03:37:08 AM
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!
Title: Re: JS accessing attributes
Post by: [Unknown] on September 18, 2004, 03:45:40 AM
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]
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 18, 2004, 12:36:20 PM
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); };
}
Title: Re: JS accessing attributes
Post by: [Unknown] on September 18, 2004, 03:11:12 PM
It must be a compatiblity thing in Firefox then; I assumed it would work.

-[Unknown]
Title: Re: JS accessing attributes
Post by: Curbow 5 on September 20, 2004, 04:01:39 PM
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;