views:

157

answers:

7

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

+2  A: 

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;
Rodrigo
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?
Oscar Godson
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
Rodrigo
Nice, you rock! that works.
Oscar Godson
+1  A: 

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.

Fredrik Hu
A: 
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 '';

qw3n
+1  A: 

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.

maksim
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
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
+2  A: 
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
Bryan Kyle
I love the default operator for really clear cases like this.
Michael T. Smith
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?
Oscar Godson
+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
A: 

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

toby
+2  A: 
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).

bobince