javascript - single line if statement -
javascript - single line if statement -
please help me. using single line javascript if statement, want execute 2 actions after has checked status not know how set sec statement. thought semicolon work, doesn't. says semicolon error. here code, semicolons...
this.classname === "unclickedbutton" ? this.classname = "clickedbutton"; $("#alert").style.display="block" : this.classname = "unclickedbutton"; $("#alert").style.display="none"
you can this, using comma operator:
this.classname === "unclickedbutton" ? (this.classname = ($("#alert").style.display="block", "clickedbutton")) : (this.classname = ($("#alert").style.display="none", "unclickedbutton")); ...but wouldn't makes readable code. i'd utilize normal if.
here's live example using jquery (source). (i used show , hide, see below). seriously, compare maintainability/readability to:
if (this.classname === "unclickedbutton") { this.classname = "clickedbutton"; $("#alert").style.display = "block"; } else { this.classname = "unclickedbutton"; $("#alert").style.display = "none"; } i know 1 i'd rather maintenance on. or if want one-liner, alter css have button class , clicked class add it, , (i'm assuming jquery here):
$("#alert").toggle($(this).toggleclass('clicked').hasclass('clicked')); live example | source
side note: selectors you're using you'd utilize jquery, jquery instances don't have style property. if you're using prototype or mootools, want remove # in front end of alert. if you're using jquery, utilize show , hide methods (and addclass / removeclass).
javascript
Comments
Post a Comment