1

I have the following mark up of a div that contains a form. The form has a basic search div and an advanced div.

 <div class="form"> <form> <input type="text" name="first_name"> <input type="text" name="last_name"> <div class="Basic" class="slide"> <input type="text" name="location"> </div> <a class="toggle-link" onclick="ShowDiv('Adv');">+ show advanced fields</a> <div id="Adv" class="slide hidden"> <input type="text" name="first_name2"> <input type="text" name="last_name2"> <input type="text" name="street"> <input type="text" name="town"> <input type="text" name="country"> </div> </form> <a onclick="ShowDiv('Basic');">- hide advanced fields</a> </div> 

The hide/show is functioning based on the following script:

 function HideDiv() { $('.slide').hide(); } function ShowDiv(ctrl) { HideDiv(); $('#' + ctrl).show(); } ShowDiv('Basic'); 

My problem (and this is a follow up ticket based on an existing question that I never managed to get to work) is that I want a user when he returns to this search form to be presented with the advanced div open if he used the advanced search form in the first place. So if any of the input fields contain any data values the div should be open when the user returns to the page.

I have this code so far but it doesn't work.

 $(document).ready(function () { $(function(){ $("div#Adv input").each(function(){ if($(this).val()!='') $(this).parent().parent().show(); return; }); }); 

3 Answers 3

1
$(function(){ if($("#Adv input[value!='']").length){ $('#Adv').show(); $('a.toggle-link').hide(); } }); 
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to worked. Now the only issue I have is that when the div is shown, so does the <a class="toggle-link" onclick="ShowDiv('Adv');">+ show advanced fields</a>. What needs to be added in your code so when the div is revealed, the class="toggle-link" is hidden
@aa1. I added it to the answer.
0

You should do

 $("div#Adv input").each(function(){ if(this.value !== ''){ //get the first ancestor with class=slide and show it $(this).closest('.slide').show(); return; } }); 

2 Comments

@aa1 that's trange because it works here jsfiddle.net/RYNRQ did you put this code inside $(document).ready()?
yes I did. I have now moved the position of the script before some other existing scripts on my page and it worked as well. Thanks
0

I believe you are missing some brackets there

$(document).ready(function(){ $(function(){ $("div#Adv input").each(function(){ if($(this).val()!='') { $(this).parent().parent().show(); return; } }); }); 

2 Comments

I have copied your code exactly on my page but still the div#Adv remains hidden no matter what. Does it matter where I place this code? At the moment it s included in the bottom of my page whereas the hide/show script is referenced from an external file.
If you want to show #Adv the use $(this).parent().show() instead of $(this).parent().parent().show()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.