user1176783
user1176783

Reputation: 673

How do I stop/break execution when selection has been found in this jquery code?

I got help with the code below from you guys on forum and the code works perfectly, but client threw a curve ball. How do I stop/break execution when selection has been found in this jquery code? They want the same fieldset to display for a couple of project type selections. But since the code continues running it turns off the FS if the same 'sect_id' is appears. How do I put in a break, so when it finds the matching value, it just stops searching?

$('fieldset#section-841', 'fieldset#section-837' ).hide();
    var DM_projtype = new Array(
        {value : 'Direct Mail', sect_id : 'fieldset#section-841'},
        {value : 'Multiples2-92', sect_id : 'fieldset#section-837'},
        {value : 'Multiples10+', sect_id : 'fieldset#section-837'}
    );
    $('select#3596').on('change',function() {
        var thisValue = $(this).val();
        var sect_id = '';
        $(DM_projtype).each(function()
        {
           $(this.sect_id).hide();
           if(this.value == thisValue)
               $(this.sect_id).show();
       });

   });
)

Upvotes: 1

Views: 65

Answers (1)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Just add return false. See below,

   $(DM_projtype).each(function()
   {
       $(this.sect_id).hide();

       if(this.value == thisValue) {
          $(this.sect_id).show();
          return false; // will break out of loop
       }

   });

Upvotes: 6

Related Questions