javascript - jQuery if statement causes browser to crash -


i have small script should show hidden div if corresponding button pressed. example, if button first-button clicked div first-button-content should show. right now, when click on button, chrome crashes , div not show.

here html:

    <div class="home-middle-buttons">         <div class="first-button">             <span>i’m in-house marketer</span>         </div>         <div class="second-button">             <span>i work @ agency</span>         </div>         <div class="third-button">             <span>i’m business owner</span>         </div>     </div>     <div class="home-middle-content">         <div class="first-button-content">             <p>lorem ipsum dolor</p>         </div>          <div class="second-button-content">             <p>lorem ipsum dolor</p>         </div>          <div class="third-button-content">             <p>lorem ipsum dolor.</p>         </div>     </div>  

here script:

jquery(document).ready(function() {     jquery(".home-middle-buttons div").click(function() {         if (jquery(".first-button").click()) {             jquery(".first-button-content").css( 'display', 'block' );         } else {             jquery(".second-button-content").css( 'display', 'block' );         }     }); }); 

your problem line:

if (jquery(".first-button").click()) { 

calling .click() triggers click event, doesn't test if click has occurred. in turn calls click handler, gets line , triggers click, calls handler, etc.

try this:

if (jquery(this).hasclass("first-button")) { 

within click handler, this refers particular button clicked, , jquery's .hasclass() returns boolean whether element has class.

but nicer way set whole thing follows:

<div class="home-middle-buttons">     <div data-associated-content="first-button-content">         <span>i’m in-house marketer</span>     </div>     <div data-associated-content="second-button-content">         <span>i work @ agency</span>     </div>     <div data-associated-content="third-button-content">         <span>i’m business owner</span>     </div> </div> <div class="home-middle-content">     <!-- in question --> </div>  

with js:

jquery(document).ready(function() {     jquery(".home-middle-buttons div").click(function() {         var associateddiv = jquery(this).attr("data-associated-content");         jquery("." + associateddiv).show();     }); }); 

demo: http://jsfiddle.net/uhztb/

note: if data-associated-content seems kind of long feel free use data-something-shorter. also, classes on content divs seem uniquely identify divs i'd suggest more logical make them id rather class.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -