keewee279
keewee279

Reputation: 1654

jQuery: How to combine two lines of code (with same target)

I am new to jQuery and am trying to combine the following two lines of code as they both target the same div ( $(this).closest('div.col-9').nextAll('div.hiddenDiv') ) and I have other, similar scenarios where this would reduce code as well:

$(this).closest('div.col-9').nextAll('div.hiddenDiv').find('input, select, textarea').removeProp('checked').removeProp('selected').not(':button, :checkbox, :hidden, :radio, :reset, :submit').val('');
$(this).closest('div.col-9').nextAll('div.hiddenDiv').hide();

It works fine the way it is written above so I am probably combining it the wrong way.
I tried both the following but they both fail:

$(this).closest('div.col-9').nextAll('div.hiddenDiv').find('input, select, textarea').removeProp('checked').removeProp('selected').not(':button, :checkbox, :hidden, :radio, :reset, :submit').val('').end().hide();

$(this).closest('div.col-9').nextAll('div.hiddenDiv').find('input, select, textarea').removeProp('checked').removeProp('selected').not(':button, :checkbox, :hidden, :radio, :reset, :submit').val('').hide();

Can someone tell me what I am doing wrong here ?

Many thanks in advance, Mike

Upvotes: 0

Views: 50

Answers (1)

Satpal
Satpal

Reputation: 133403

You should call the $.fn.hide() method first then find children and preform required operation.

$(this)
    .closest('div.col-9')
    .nextAll('div.hiddenDiv')
    .hide()  //<=-----------------------   Call Hide method
    .find('input, select, textarea')
    .removeProp('checked')
    .removeProp('selected')
    .not(':button, :checkbox, :hidden, :radio, :reset, :submit')
    .val('');

Upvotes: 2

Related Questions