I’m trying to make a search form. I want it to be like when I click in the search form opener button, the search form would show and its width would be 100%.
When I click it again the search form width would return to 0. The first one is working but when I click it back, the form is not set to width of 0.
app.js :
JavaScript
x
56
56
1
var header_right_search_btn =$("#header_right_search_btn");
2
var header_search_cont = $("#header_search_cont");
3
var search_box = $("#search_box");
4
header_right_search_btn.click(function(e){
5
e.preventDefault();
6
if(header_search_cont.css("width","0")){
7
header_search_cont.css("width","100%")
8
search_box.focus();
9
header_search_cont.css("opacity","1");
10
header_search_cont.css("transition","all .2s ease-in-out");
11
}else if(header_search_cont.css("width","100%")){
12
header_search_cont.css("width","0")
13
header_search_cont.css("opacity","0");
14
header_search_cont.css("transition","all .2s");
15
16
}
17
console.log("hhihihihi");
18
19
})
20
21
var search_area_die = $("#search_area_die");
22
search_area_die.click(function(e){
23
e.preventDefault();
24
header_search_cont.css("width","0")
25
header_search_cont.css("opacity","0");
26
header_search_cont.css("transition","all .2s");
27
})
28
//search ends
29
30
31
32
<div id="header_search_cont" class="unstreched_search">
33
<form action="" method="post" name="search_form" id="search_form" enctype="multipart/form-data">
34
<button type="button" id="search_area_die"><svg id="search_die_svg" version="1.1" id="Layer_1"
35
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
36
viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
37
<g>
38
<g>
39
<path
40
d="M501.333,245.333H36.417l141.792-141.792c4.167-4.167,4.167-10.917,0-15.083c-4.167-4.167-10.917-4.167-15.083,0l-160,160c-4.167,4.167-4.167,10.917,0,15.083l160,160c2.083,2.083,4.813,3.125,7.542,3.125c2.729,0,5.458-1.042,7.542-3.125c4.167-4.167,4.167-10.917,0-15.083L36.417,266.667h464.917c5.896,0,10.667-4.771,10.667-10.667S507.229,245.333,501.333,245.333z" />
41
</g>
42
</g>
43
</svg></button>
44
<input type="search" name="search_box" id="search_box" placeholder="Search..."><button id="searchbtn"
45
name="searchbtn"><svg class="search_ico_svg" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg"
46
xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512"
47
style="enable-background:new 0 0 512 512;" xml:space="preserve">
48
<g>
49
<g>
50
<path d="M508.875,493.792L353.089,338.005c32.358-35.927,52.245-83.296,52.245-135.339C405.333,90.917,314.417,0,202.667,0S0,90.917,0,202.667s90.917,202.667,202.667,202.667c52.043,0,99.411-19.887,135.339-52.245l155.786,155.786c2.083,2.083,4.813,3.125,7.542,3.125c2.729,0,5.458-1.042,7.542-3.125C513.042,504.708,513.042,497.958,508.875,493.792zM202.667,384c-99.979,0-181.333-81.344-181.333-181.333S102.688,21.333,202.667,21.333S384,102.677,384,202.667
51
S302.646,384,202.667,384z" />
52
</g>
53
</g>
54
</svg></button></form>
55
</div>
56
Advertisement
Answer
Simple way is to use addClass()
and removeClass()
functions instead of using .css
Then for if statement you can use if($(selector).hasClass())
JavaScript
1
21
21
1
var header_right_search_btn =$("#header_right_search_btn");
2
var header_search_cont = $("#header_search_cont");
3
var search_box = $("#search_box");
4
header_right_search_btn.click(function(e){
5
e.preventDefault();
6
if(!header_search_cont.hasClass('strech')){ //<< if not hasClass() strech
7
search_box.focus();
8
header_search_cont.addClass('strech'); // addClass()
9
}else{
10
header_search_cont.removeClass('strech'); // removeClass()
11
}
12
console.log("hhihihihi");
13
14
})
15
16
var search_area_die = $("#search_area_die");
17
search_area_die.click(function(e){
18
e.preventDefault();
19
header_search_cont.removeClass('strech'); //<< removeClass()
20
})
21
//search ends
JavaScript
1
14
14
1
#header_search_cont{
2
width : 0px;
3
opacity : 0;
4
transition : all .2s ease-in-out;
5
overflow : hidden;
6
background : red;
7
padding : 10px;
8
box-sizing : border-box;
9
}
10
#header_search_cont.strech{
11
width : 100%;
12
opacity : 1;
13
transition : all .2s ease-in-out;
14
}
JavaScript
1
8
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div id="header_right_search_btn">Open Search</div>
3
<div id="header_search_cont">
4
<form>
5
<input id="search_box" type="search" placeholder="search" />
6
<input id="search_area_die" type="submit" value="search"/>
7
</form>
8
</div>
Explanation:
Add/remove class
strech
to the element on clickCheck for
strech
class by using.hasclass('strech'))
. The!
mark meansNOT
so!$(selector).hasClass('strech')
means if the element doesn’t have this class