469,631 Members | 1,123 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

getting active element

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
Jun 16 '07 #1
8 2128

"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:ie******************@newssvr11.news.prodigy.n et...
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)
Jun 16 '07 #2
Jon Slaughter wrote:
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:ie******************@newssvr11.news.prodigy.n et...
>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.

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.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 16 '07 #3

"-Lost" <ma****************@techie.comwrote in message
news:68******************************@comcast.com. ..
Jon Slaughter wrote:
>"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:ie******************@newssvr11.news.prodigy. net...
>>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.

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.
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[i].nodeName=="DIV")
{
if (k == loc) (val[i].style.display == 'none') ?
val[i].style.display = 'block' : val[i].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
Jun 16 '07 #4
Jon Slaughter said the following on 6/16/2007 3:16 PM:
"-Lost" <ma****************@techie.comwrote in message
news:68******************************@comcast.com. ..
>Jon Slaughter wrote:
>>"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:ie******************@newssvr11.news.prodigy .net...
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?
<snip>
>1. Do not use the "javascript:" protocol in links. In fact, do not use
it period unless you are writing a bookmarklet.

Why?
<URL: http://jibbering.com/faq/index.html#FAQ4_24>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 16 '07 #5
Jon Slaughter wrote:
"-Lost" <ma****************@techie.comwrote in message
news:68******************************@comcast.com. ..
>Jon Slaughter wrote:
>>"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:ie******************@newssvr11.news.prodigy .net...
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.
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? ; )

?
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...
>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?
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).

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 17 '07 #6

"-Lost" <ma****************@techie.comwrote in message
news:Cb******************************@comcast.com. ..
Jon Slaughter wrote:
>"-Lost" <ma****************@techie.comwrote in message
news:68******************************@comcast.com ...
>>Jon Slaughter wrote:
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:ie******************@newssvr11.news.prodig y.net...
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.
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? ; )

?

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 ;/


Jun 17 '07 #7
Jon Slaughter wrote:
"-Lost" <ma****************@techie.comwrote in message
news:Cb******************************@comcast.com. ..
>Jon Slaughter wrote:
>>"-Lost" <ma****************@techie.comwrote in message
news:68******************************@comcast.co m...
Jon Slaughter wrote:
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:ie******************@newssvr11.news.prodi gy.net...
>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.
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? ; )

?
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 ;/
Don't feel bad. I have a disability directly related to memory. :(

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 17 '07 #8
On Jun 17, 5:16 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:
[...]
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[i].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[i];
if (val.nodeName.toLowerCase() == 'div'){
{
if (k == loc) (val[i].style.display == 'none') ?
val[i].style.display = 'block' : val[i].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.
--
Rob

Jun 17 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Wm | last post: by
4 posts views Thread by crhaynes | last post: by
4 posts views Thread by Stephen Poley | last post: by
7 posts views Thread by McKirahan | last post: by
1 post views Thread by tangus via DotNetMonster.com | last post: by
11 posts views Thread by Derek Martin | last post: by
9 posts views Thread by newcomsas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.