homepage Welcome to WebmasterWorld Guest from 23.20.61.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript 'this' keyword
how flexible is it?
rpking

10+ Year Member



 
Msg#: 93 posted 2:16 pm on Apr 29, 2002 (gmt 0)

I don't think that this is possible in any easy manner, but I'm hoping somebody can surprise me...

If I pass the this obj to a function via an eventhandler, it gives me the current object, such as a checkbox in the form.

However, I would like to affect the next and previous elements in the elements array, not the one that fired the event.

So, is there an easy way to get to document.form.elements[this + n], without looping through all the form elemenst in turn, comparing them to the current one?

 

rpking

10+ Year Member



 
Msg#: 93 posted 2:36 pm on Apr 29, 2002 (gmt 0)

Having read my question, I'm not too sure how clear i was. Here is a bit of example code to illustrate my problem...

-----HTML-------
<form>
<input type="checkbox">
<input type="checkbox" onClick="JavaScript:clear(this);">
<input type="checkbox">
</form>

-----SCRIPT-----
function clear(obj){
?????????????????????????
}

I would like my function to clear the checkboxes to either side of the one clicked, using only the information passed via 'this'. I cannot use the names or ids of the checkboxes, just their element array ID from document.forms[0].elements[x], where x is the id.

Am I attempting the impossible?

joshie76

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 93 posted 12:26 pm on May 1, 2002 (gmt 0)

Hi rpking, sorry for the late reply...

I'm sure you've found a workaround by now but just for the record:

You can't use this+n as this passes the whole object (the HTML element, it's contents and attributes) to the function. Therefore this+n is like saying String([object]+n) which doesn't make any sense to the interpreter.

I had a tinker with a number of advanced DOM methods (such as previousSibling and so on) without success. What you're doing does seem a little unusual. You could of course loop through all the elements of the form and do whatever you need:

<form>
<input type="checkbox">
<input type="checkbox" onClick="JavaScript:goClear(this);">
<input type="checkbox">
</form>

<script>
function goClear(obj)
{
for (var i=0; i < obj.form.elements.length; i++)
{
//make sure we don't modify the element that called the function
if (obj != obj.form.elements[i])
{
obj.form.elements[i].checked = false; //or whatever...
}
}
}
</script>

Otherwise you are going to struggle without some identifying feature of the elements you want to modify (id or name). Where did you get with this?

Also, don't use the function name clear - it's a JavaScript keyword.

rpking

10+ Year Member



 
Msg#: 93 posted 1:37 pm on May 1, 2002 (gmt 0)

Cheers for the reply Joshie. I though this one would be tricky/impossible...

Still not really got a solution. The basic premise is that due to server side code, all checkboxes have the same name. This makes them accesible as an array server side, where we are only interested in their state.

However, client side script is needed that allows interaction between certain checkboxes, ie checking one, has to uncheck others.

On any one page there could be loads of form elements (easily running into the hundreds). This means that looping through them all is too intensive for every click.

I'm thinking that the best way to achieve the desired affect is to pass through the number of the checkbox, which will tell me where in the elements array it lives.

This will mean quite a bit rewriting of the page code to produce the correct labels :-(

moonbiter

10+ Year Member



 
Msg#: 93 posted 9:39 pm on May 1, 2002 (gmt 0)

Still not really got a solution. The basic premise is that due to server side code, all checkboxes have the same name. This makes them accesible as an array server side, where we are only interested in their state.

However, client side script is needed that allows interaction between certain checkboxes, ie checking one, has to uncheck others.

I assume you mean checking one has to uncheck some of the others, but not *all* of the others. Otherwise you could use radio buttons, yes?

Purple Martin

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 93 posted 5:58 am on May 2, 2002 (gmt 0)

>> due to server side code, all checkboxes have the same name

This doesn't have to be the case - you'll be using a counter in the server-side loop that's writing the checkboxes, right? Just concatenate the counter value to the end of each checkbox name to get unique names.

Server side for the name attribute looks something like:

name="myChk<%= i %>"

Client side gets name attributes looking something like:

name="myChk0"
name="myChk1"
name="myChk2"

rpking

10+ Year Member



 
Msg#: 93 posted 8:13 am on May 2, 2002 (gmt 0)

> uncheck some of the others, but not *all* of the others
Correct moonbiter.

> This doesn't have to be the case
I know, but in the system I have to work with, it does. I know it sounds stupid, but that's the way it is. More weight is given to the guys who write the server side stuff, so I'm left to pick up the pieces. A lot of the server side code is tied up in compiled code as well, so tough to change.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved