getting active element

J

Jon Slaughter

if I call javascript like

<a href="javascript:;" onmousedown="toggle('a');">O</a>

is there a way to pass the element a to the function without specifying any
tags or names?

I want the function to automatically return a "pointer" to the a link that
it was called from instead of having to specify it.

Thanks,
Jon
 
J

Jon Slaughter

Jon Slaughter said:
if I call javascript like

<a href="javascript:;" onmousedown="toggle('a');">O</a>

is there a way to pass the element a to the function without specifying
any tags or names?

I want the function to automatically return a "pointer" to the a link that
it was called from instead of having to specify it.

Thanks,
Jon

I think I can just pass this and it will work? (atleast the example I tried
works)
 
L

-Lost

Jon said:
I think I can just pass this and it will work? (atleast the example I tried
works)

Hey Jon. A little far from home aren't you? ; )

Anyway, 2 things.

1. Do not use the "javascript:" protocol in links. In fact, do not use
it period unless you are writing a bookmarklet.

2. Yes, you can use "this" as "this" refers intrisicly to the calling
object.

3. There are more appropriate events for a link, for example,
"onclick." Unless of course you specifically need the precision of a
mouse down (as in, do you need mousedown, then mouseup).

An example:

<a href="page_to_go_to_if_javascript_is_disabled.htm"
onclick="toggle(this);">link</a>

Bear this in mind, it passes the link as an object. That is, your
toggle function will need to handle an object. "this" will not pass
just the links name, or href or whatever for example. It will pass the
entire link with properties.
 
J

Jon Slaughter

-Lost said:
Hey Jon. A little far from home aren't you? ; )

?

Anyway, 2 things.

1. Do not use the "javascript:" protocol in links. In fact, do not use
it period unless you are writing a bookmarklet.

Why?

2. Yes, you can use "this" as "this" refers intrisicly to the calling
object.

3. There are more appropriate events for a link, for example, "onclick."
Unless of course you specifically need the precision of a mouse down (as
in, do you need mousedown, then mouseup).


Actually I'm not using a link. I jus want to toggle the display state of an
object that is related to some other object. I have done this

function toggleDisplay(id, loc)
{
var val = id.parentNode.childNodes;
var loc = (loc == null) ? 0 : loc;

for (i=0; i<val.length; i++)
{
var k = 0;
if (val.nodeName=="DIV")
{
if (k == loc) (val.style.display == 'none') ?
val.style.display = 'block' : val.style.display = 'none';
k++;
}
}
return;
}

Which lets me toggle a div that is a certain sibling of some object. (for
some reason I couldn't get nextSibling to work right(had to use it twice to
get the adjacient div and I guess it had to do with text blocks or
something)).


Anyways, what I'm using does seem to work. I'd rather have used css to do
the toggling but guess thats impossible ;/

Thanks,
Jon
 
L

-Lost

Jon said:

Um, we've conversed and I've shared code with you in comp.lang.php.

Damn, am I that forgettable, it was like a week ago?

Oh well...

Mr. Webb has provided you the relevant link.
Anyways, what I'm using does seem to work. I'd rather have used css to do
the toggling but guess thats impossible ;/

"CSS popups" is (I think) the closest thing you'll come to that, but
never a true toggle (with CSS).
 
J

Jon Slaughter

-Lost said:
Um, we've conversed and I've shared code with you in comp.lang.php.

Damn, am I that forgettable, it was like a week ago?

Oh well...

Naw... its just that my memory sucks ;/
 
R

RobG

Actually I'm not using a link. I jus want to toggle the display state of an
object that is related to some other object. I have done this

function toggleDisplay(id, loc)
{
var val = id.parentNode.childNodes;

If you only want divs, then you might try:

var divs = id.parentNode.getElementsByTagName('div');


It may not be what you want as it will get all the descendant divs,
not just the direct children of parentNode.

var loc = (loc == null) ? 0 : loc;

If no value is passed to loc, its value will be 'undefined'. So while
the above "works", it isn't strictly true. You may prefer:

var loc = loc || 0;

so that if loc evaluates to false (i.e. it is undefined, false, null
or zero) it will be assigned a value of zero. Otherwise, it is
assigned its current value.
for (i=0; i<val.length; i++)

You should keep i local with var, and getting val.length every time is
inefficient, consider:

for (var i=0, len=val.length; i<len; i++)

{
var k = 0;

There's no need to declare k on every loop (it doesn't do any harm,
it's just not necessary), you can declare it outside and just set its
value here.

if (val.nodeName=="DIV")


It's better to store values if they are going to be used a number of
times rather than looking them up. Declare val earlier (say at the
start with k), then:

val = val;
if (val.nodeName.toLowerCase() == 'div'){
{
if (k == loc) (val.style.display == 'none') ?
val.style.display = 'block' : val.style.display = 'none';


or

val.style.display = (val.style.display == 'none')? '' : 'none';

k++;
}
}
return;

If there is no return value, there's no need for return here.
}

Which lets me toggle a div that is a certain sibling of some object. (for
some reason I couldn't get nextSibling to work right(had to use it twice to
get the adjacient div and I guess it had to do with text blocks or
something)).

Some browsers insert #text nodes to preserve whitespace, e.g. Given
the following HTML:

<div>
<p>
<p>
</div>

in firefox the div will have 3 child nodes (#text, p, p), in IE there
will only be 2 (p, p). So you can happily use nextSibling if you
filter out the nodes you don't want (which is what you are doing with
the childNodes collection).

Anyways, what I'm using does seem to work. I'd rather have used css to do
the toggling but guess thats impossible ;/

Not at all, you can add/remove class values or edit the CSS rule
directly, take your pick. Where you do the display:none bit in you
current function, you could add or remove a class instead. It is
frequently used where more than a simple 'hide' is required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top