Skip to main content
added 2 characters in body; edited tags; edited title
Source Link
bluish
  • 27.5k
  • 28
  • 126
  • 185

If elementone of two elements exists, do X (jQuery)something

I currently do this to check if anone of two elements exists:

if ($(".element1").length > 0 || $(".element2").length > 0) { ...//do stuff... } 

Is there a better way to rewrite the same? I mean, ".length"is .length the same as ".length > 0" .length > 0?

Thanks

If element exists, do X (jQuery)

I currently do this to check if an elements exists:

if ($(".element1").length > 0 || $(".element2").length > 0) { ...stuff... } 

Is there a better way to rewrite the same? I mean, ".length" the same as ".length > 0" ?

Thanks

If one of two elements exists, do something

I currently do this to check if one of two elements exists:

if ($(".element1").length > 0 || $(".element2").length > 0) { //do stuff... } 

Is there a better way to rewrite the same? I mean, is .length the same as .length > 0?

added the missing close bracket )
Source Link
Deanna
  • 24.4k
  • 7
  • 76
  • 162

I currently do this to check if an elements exists:

if ($(".element1").length > 0 || $(".element2").length > 0) { ...stuff... } 

Is there a better way to rewrite the same? I mean, ".length" the same as ".length > 0" ?

Thanks

UPDATE [Added the missing bracket. And the stupid stackoverflow-editing-form does not allow to add less than 6 characters, so I have to write this text]

I currently do this to check if an elements exists:

if ($(".element1").length > 0 || $(".element2").length > 0) { ...stuff... } 

Is there a better way to rewrite the same? I mean, ".length" the same as ".length > 0" ?

Thanks

UPDATE [Added the missing bracket. And the stupid stackoverflow-editing-form does not allow to add less than 6 characters, so I have to write this text]

I currently do this to check if an elements exists:

if ($(".element1").length > 0 || $(".element2").length > 0) { ...stuff... } 

Is there a better way to rewrite the same? I mean, ".length" the same as ".length > 0" ?

Thanks

I currently do this to check if an elements exists:

if ($(".element1").length > 0 || $(".element2").length > 0) { ...stuff... } 

Is there a better way to rewrite the same? I mean, ".length" the same as ".length > 0" ?

Thanks

UPDATE [Added the missing bracket. And the stupid stackoverflow-editing-form does not allow to add less than 6 characters, so I have to write this text]

I currently do this to check if an elements exists:

if ($(".element1").length > 0 || $(".element2").length > 0 { ...stuff... } 

Is there a better way to rewrite the same? I mean, ".length" the same as ".length > 0" ?

Thanks

I currently do this to check if an elements exists:

if ($(".element1").length > 0 || $(".element2").length > 0) { ...stuff... } 

Is there a better way to rewrite the same? I mean, ".length" the same as ".length > 0" ?

Thanks

UPDATE [Added the missing bracket. And the stupid stackoverflow-editing-form does not allow to add less than 6 characters, so I have to write this text]

Source Link
eozzy
  • 69.4k
  • 111
  • 287
  • 448
Loading