Do I have to check that an element exsist to get their properties?

49 views
Skip to first unread message

Hamburger

unread,
Oct 3, 2012, 6:36:01 AM10/3/12
to mootool...@googlegroups.com
Hello,
please let me have one more beginner question.
With the following test I will get an error if the 'a' - element doesnt exsist.
var test = this.getElement("a").get("href");

What I would like to have is a 'null' or 'undefined'

Do I have todo it realy like this:
var test = this.getElement("a");
if (test) {test=test.get("href");}

or is there a shorter other way to do this.

Arian Stolwijk

unread,
Oct 3, 2012, 6:39:34 AM10/3/12
to mootool...@googlegroups.com
$() and .getElement() can return null if the element doesn't exist.
So if you're not sure the element exists, you'll have to check it indeed with an if statement.

Dimitar Christoff

unread,
Oct 3, 2012, 8:45:27 AM10/3/12
to mootool...@googlegroups.com
you can be lazy and do:

var prop = thisl.getElements('a').get('href').getLast();

it's not the most performant.
--
Dimitar Christoff

"JavaScript is to JAVA what hamster is to ham"
@D_mitar - https://github.com/DimitarChristoff

Philip Thompson

unread,
Oct 3, 2012, 10:43:19 AM10/3/12
to mootool...@googlegroups.com
You could create a wrapper method to handle this extra logic for you. Something like (untested)...

Element.implement({
    getSomething: function(tag, something) {
        var el = this.getElement(tag);
        return el ? el.get(something) : null;
    }
});

var href = this.getSomething('a', 'href');

~Philip

Sanford Whiteman

unread,
Oct 3, 2012, 10:42:18 PM10/3/12
to Hamburger
> or is there a shorter other way to do this.

var defaultanchor = new Element('a');

... later ...

var alwaysfindanhref = this.getElement("a") || defaultanchor.get("href");

If this.getElement('a) finds something in the DOM, you get its href.

If this.getElement has no result, you get a null (or, if you set an
href when creating defaultelement, that'll be your default result).

Note that you can use this concept in one line if somehow mandatory...

var alwaysfindanhref = this.getElement("a") || new Element("a").get("href");

... but that's creating a new Element every time, which uses memory.

-- Sandy

Barry van Oudtshoorn

unread,
Oct 3, 2012, 11:16:56 PM10/3/12
to mootool...@googlegroups.com
Small nit: you'll need to use parens to have that work correctly, like so:

var alwaysfind = (this.getElement('a') || defaultanchor).get('href');

Otherwise, if |this.getElement('a')| finds an element, you'll get the
element, not its href.

>> or is there a shorter other way to do this.
> var alwaysfindanhref = this.getElement("a") || defaultanchor.get("href");

--
Barry van Oudtshoorn
www.barryvan.com.au

Sanford Whiteman

unread,
Oct 3, 2012, 11:23:49 PM10/3/12
to Barry van Oudtshoorn
Yep, oopsie there. Same idea.

-- S.

Dimitar Christoff

unread,
Oct 4, 2012, 1:16:38 PM10/4/12
to mootool...@googlegroups.com
> var alwaysfind = (this.getElement('a') || defaultanchor).get('href');

this kind of code, although possible, makes no sense and won't be
testable. how do you know if the link property has arrived from
element 1 or the default element? it may not matter but this is not a
good pattern to base any logic upon and may later on introduce bugs
that are difficult to trace.

so, for my money - it may be more typing to check element existence
and it may involve if/else blocks but it allows you to properly react
to both cases and also allows you to later change and extend the
logic.

best regards

Sanford Whiteman

unread,
Oct 4, 2012, 1:34:42 PM10/4/12
to mootool...@googlegroups.com
Makes no less or more sense than when you have default options for a class (or function) and you don't care whether an instance got a given value from the default or from passed arguments.

Eek!

Or not.

Sometimes it doesn't matter. You use the best code for the job and I occasionally -- very occasionally -- use this pattern and "makes no sense" is hardly a convincing argument, since you in fact know exactly how and why it works.

-- S.

Sanford Whiteman

unread,
Oct 4, 2012, 1:40:10 PM10/4/12
to mootool...@googlegroups.com
Also, the OP makes no distinction between an extant A tag w/no href and no A, saying he "would like to have is a 'null' or 'undefined'" as a result of the one-liner. If that's the spec, you fit the spec. It's not like we're getting any context here at all.

-- S.

Hamburger

unread,
Oct 5, 2012, 7:46:08 AM10/5/12
to mootool...@googlegroups.com, sa...@figureone.com
Hello,
thanks for this discussion.
I mentioned it because I saw this jquery statement:
var headLink = $this.find("a:first").attr("href");
If the a-tag doesn't exsist it gives an undefined
[
"headLink: ", undefined]

cheers hamburger

Sanford Whiteman

unread,
Oct 5, 2012, 2:57:48 PM10/5/12
to Hamburger
> I mentioned it because I saw this jquery statement:
> var headLink = $this.find("a:first").attr("href");
> If the a-tag doesn't exsist it gives an undefined
> ["headLink: ", undefined]

jQuery or Moo or plain JS-- you'll find that native DOM methods, and
the DOM wrappers that frameworks implement, return null when elements
are not found.

This makes chaining from these methods difficult because they will
either return a very exciting object *or* something that will kill
your function().function().function() chain in its tracks.

So you have to be sensitive to these conditions in some way. Either
step through with intermediate variables (preferable) or do something
wacky but fun using the || operator as I suggested. But you really
should come to an understanding of why each option works if you're
going to understand JS.

*Note that in theory, a third way to deal with null.function()
condition is via SEH (try-catch blocks). The problem is that while
this kind of exception will be handled, the `message` text is
different from browser to browser, and there's no standard code, so
your code can't react intelligently to it. For example, Gecko will say
"TypeError: ... is null", while Presto will say "TypeError: cannot
convert to ... to object". Too freeform to be useful; may not be
properly classed as an exception anyway. But again, good to know what
does and doesn't work.

-- S.


Reply all
Reply to author
Forward
0 new messages