0

if $('.item:last').attr('id').split('-')[1] is not undefined var liid equals that, and if it is undefined then it equals null

4
  • 2
    Are you familiar with the if else language construct? Jul 12, 2010 at 19:28
  • ... yes. I know, but i like to keep my code succinct and for something like this, knowing other langs, i figured like all the other people answering below, there was a shorter and cleaner way of writing it rather than declaring my var, then checking, then setting, just do it all in one line. Jul 12, 2010 at 20:01
  • Isn't a dash/minus sign forbidden in an element id?
    – kennebec
    Jul 12, 2010 at 23:01
  • Yes, you can use dashes. You just can't start it with a number. Jul 15, 2010 at 23:52

7 Answers 7

3
var liid = $('.item:last').attr('id').split('-')[1] || null;

This In JavaScript the logical-or (||) operator works differently than you might be used to. || will return the its left operand if it's something "truth-y". A truth-y value is one of the following:

  • An object
  • Any number != 0
  • true
  • A non-empty string
3
  • I love the default operator for really clear cases like this. Jul 12, 2010 at 19:56
  • Not sure why... i feel stupid, but why am i getting a "$('.item:last').attr('id') is undefined". Obv because the attr doesn't exist, but isn't that what we're checking for? Why is it erroring out? Jul 12, 2010 at 20:08
  • +1 for shortest way. I'm not used to the || or being a kind of if statement. Though it is common in the var e || or e.event
    – qw3n
    Jul 12, 2010 at 22:46
2

assuming that .item:last is defined:

var liid = typeof $('.item:last').attr('id').split('-')[1] != undefined ? $('.item:last').attr('id').split('-')[1] : null;

otherwise:

var liid = $('.item:last').length > 0 ? $('.item:last').attr('id').split('-')[1] : null;
2
  • Not sure why... i feel stupid, but why am i getting a "$('.item:last').attr('id') is undefined". Obv because the attr doesn't exist, but isn't that what we're checking for? Why is it erroring out? Jul 12, 2010 at 20:07
  • hm probably because the whole $('.item:last').attr('id') is undefined, this snippet assumes that at least one item is set, so in that case you would need to split it
    – Rod
    Jul 12, 2010 at 20:09
2
var parts= $('.item').last().attr('id').split('-');
var liid= parts.length>=1? parts[1] : null;
  • avoid :last and the other jQuery-specific non-standard selectors when you can, as they will cause the relatively slow ‘Sizzle’ selector engine to be used instead of the fast browser in-built querySelectorAll method on modern browsers. Use jQuery navigation functions instead in preference.

  • whilst you can use || null as in the previous answers to get a shorter expression, this has caveats that may or may not affect you. Personally I prefer to be explicit about what test I'm doing.

    In particular, the empty string would also be ‘falsy’ and so would cause the null value to be returned if the id happened to be a--b.

If what your question is asking is how to write this in ‘JavaScript’, ie. without jQuery:

var els= document.getElementsByClassName('item');
var parts= els[els.length-1].id.split('-');
...

however this uses the HTML5 getElementsByClassName method, which not all browsers support yet, so you'd have to add in fallback support for that (see previous questions on this subject).

1

Michael Mrozek is correct. You wouldn't want to do the entire lookup twice.

Just do

var liid = (typeof($('.item:last').attr('id').split('-')[1]);
liid = typeof(liid) == 'undefined' ? null : liid;

or do the if statement.

1

var value = $('.item:last').attr('id').split('-')1;
(value == undefined) ? liid = value : liid = null;

The operator is called a ternary conditional operator, or, sometimes referred to as the ternary operator. Info on undefined, null, etc. is here.

Also, I stored $('.item:last').attr('id').split('-')1 in a variable so that the next time I wanted to get that value, the machine wouldn't have to recalculate it by calling multiple functions; a simple read from memory would be enough.

2
  • More specifically, it's the conditional operator. It so happens to be the only ternary -- i.e., 3-operand -- operator in the language, but referring to it as such doesn't really give any clue as to the meaning and usage of either the operator or the word "ternary". For example, '=' is a binary operator. But if you were explaining what it is, how it works, etc., it's more useful to refer to it as "the assignment operator" rather than "a binary operator".
    – Vineet
    Jul 12, 2010 at 20:20
  • Sorry about that. I've always been taught that it's called the ternary operator. I'll edit my post to make more sense.
    – maksim
    Jul 16, 2010 at 17:33
0
var liid=$('.item:last').attr('id').split('-')[1];
liid?'':liid=null;

This will only work however if $('.item:last').attr('id').split('-')[1] when not undefined equals something other than the values that equal false such as 0 or an empty string '';

0

Just append || null behind the expression to use it as the default value:

var liid = $('.item:last').attr('id').split('-')[1] || null
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.