Live search for accordions and its contentAdding HTML entities using CSS contentjQuery/JavaScript: accessing contents of an iframeHow to make div not larger than its contents?How to align content of a div to the bottom?Add content to jQuery accordion section dynamicallyCannot display HTML stringAccordion not working bootstrapAccordion Next ButtonSearch table and return correct table rows on button click using javascript/jQuery and the contents of the 'search' barfadeOut() parent element comes back

What's the logic behind the the organization of Hamburg's bus transport into "rings"?

Removing applications from Show Applications without uninstalling

Pros and cons of writing a book review?

How to skip replacing first occurrence of a character in each line?

What happened to all the nuclear material being smuggled after the fall of the USSR?

How can drunken, homicidal elves successfully conduct a wild hunt?

X-shaped crossword

Why is the relationship between frequency and pitch exponential?

Secure offsite backup, even in the case of hacker root access

My coworkers think I had a long honeymoon. Actually I was diagnosed with cancer. How do I talk about it?

If Boris Johnson were prosecuted and convicted of lying about Brexit, can that be used to cancel Brexit?

Does Peach's float negate shorthop knockback multipliers?

Did Darth Vader wear the same suit for 20+ years?

Adding two lambda-functions in C++

Who operates delivery flights for commercial airlines?

What's the correct term for a waitress in the Middle Ages?

Working in the USA for living expenses only; allowed on VWP?

Company did not petition for visa in a timely manner. Is asking me to work from overseas, but wants me to take a paycut

What is the traditional way of earning a doctorate in Germany?

What happens if you do emergency landing on a US base in middle of the ocean?

Identification quotas - TIKZ LaTeX

Should I "tell" my exposition or give it through dialogue?

Is it possible for people to live in the eye of a permanent hypercane?

Opposite of "Squeaky wheel gets the grease"



Live search for accordions and its content


Adding HTML entities using CSS contentjQuery/JavaScript: accessing contents of an iframeHow to make div not larger than its contents?How to align content of a div to the bottom?Add content to jQuery accordion section dynamicallyCannot display HTML stringAccordion not working bootstrapAccordion Next ButtonSearch table and return correct table rows on button click using javascript/jQuery and the contents of the 'search' barfadeOut() parent element comes back






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I have a list of accordions in my html page and i would like to have a search box for filtering the accordions and its contents. Basically i would like to create a search box for all the accordions but i also want to search for the contents inside the accordions. Sorry if my question is not clear but this is my first question on Stackoverflow.



Below the code is for how i am creating the accordions and how i show them when the user press one title. (i have created one accordion in the html and i clone it whenever i want to create more in the javascript file.)



// this is in the html



<input type="search" id="accordion_search_bar" placeholder="Search"/>

<div id="accordions">
<div id="accID1" class="AccordionContainer">
<button id="accID" class="accordion"></button>
<div class="panel" id="panel1">
</div>


//this is the js file



for (a = 0; a < acc.length; a++) 
acc[a].addEventListener("click", function()
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight)
panel.style.maxHeight = null;
else
panel.style.maxHeight = panel.scrollHeight + "px";

);



// this is the other js for the search



$(function() 
var searchTerm, panelContainerId;
// Create a new contains that is case insensitive
$.expr[":"].containsCaseInsensitive = function(n, i, m)
return (
jQuery(n)
.text()
.toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0
);
;

$("#accordion_search_bar").on("change keyup paste click", function()
searchTerm = $(this).val();
$("#accordions > .AccordionContainer").each(function()
panelContainerId = "#" + $(this).attr("id");
$(
panelContainerId + ":not(:containsCaseInsensitive(" + searchTerm + "))"
).hide();
$(
panelContainerId + ":containsCaseInsensitive(" + searchTerm + ")"
).show();
);
);
);


Basically i want to the search box to search through the buttons of all accordions and also search in every panel that i created for each accordion.










share|improve this question
























  • Can you please provide some HTML with contents and the searchbox so that we can see better what you want to achieve? Moreover this is not jQuery what you are using here.

    – Gutelaunetyp
    Mar 24 at 15:20












  • I edit and put the search box and the jquery code that i am using for the search. My problem is that the code isnt working as expected when i type something in the search box nothing happens

    – Dts742
    Mar 24 at 16:45


















2















I have a list of accordions in my html page and i would like to have a search box for filtering the accordions and its contents. Basically i would like to create a search box for all the accordions but i also want to search for the contents inside the accordions. Sorry if my question is not clear but this is my first question on Stackoverflow.



Below the code is for how i am creating the accordions and how i show them when the user press one title. (i have created one accordion in the html and i clone it whenever i want to create more in the javascript file.)



// this is in the html



<input type="search" id="accordion_search_bar" placeholder="Search"/>

<div id="accordions">
<div id="accID1" class="AccordionContainer">
<button id="accID" class="accordion"></button>
<div class="panel" id="panel1">
</div>


//this is the js file



for (a = 0; a < acc.length; a++) 
acc[a].addEventListener("click", function()
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight)
panel.style.maxHeight = null;
else
panel.style.maxHeight = panel.scrollHeight + "px";

);



// this is the other js for the search



$(function() 
var searchTerm, panelContainerId;
// Create a new contains that is case insensitive
$.expr[":"].containsCaseInsensitive = function(n, i, m)
return (
jQuery(n)
.text()
.toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0
);
;

$("#accordion_search_bar").on("change keyup paste click", function()
searchTerm = $(this).val();
$("#accordions > .AccordionContainer").each(function()
panelContainerId = "#" + $(this).attr("id");
$(
panelContainerId + ":not(:containsCaseInsensitive(" + searchTerm + "))"
).hide();
$(
panelContainerId + ":containsCaseInsensitive(" + searchTerm + ")"
).show();
);
);
);


Basically i want to the search box to search through the buttons of all accordions and also search in every panel that i created for each accordion.










share|improve this question
























  • Can you please provide some HTML with contents and the searchbox so that we can see better what you want to achieve? Moreover this is not jQuery what you are using here.

    – Gutelaunetyp
    Mar 24 at 15:20












  • I edit and put the search box and the jquery code that i am using for the search. My problem is that the code isnt working as expected when i type something in the search box nothing happens

    – Dts742
    Mar 24 at 16:45














2












2








2








I have a list of accordions in my html page and i would like to have a search box for filtering the accordions and its contents. Basically i would like to create a search box for all the accordions but i also want to search for the contents inside the accordions. Sorry if my question is not clear but this is my first question on Stackoverflow.



Below the code is for how i am creating the accordions and how i show them when the user press one title. (i have created one accordion in the html and i clone it whenever i want to create more in the javascript file.)



// this is in the html



<input type="search" id="accordion_search_bar" placeholder="Search"/>

<div id="accordions">
<div id="accID1" class="AccordionContainer">
<button id="accID" class="accordion"></button>
<div class="panel" id="panel1">
</div>


//this is the js file



for (a = 0; a < acc.length; a++) 
acc[a].addEventListener("click", function()
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight)
panel.style.maxHeight = null;
else
panel.style.maxHeight = panel.scrollHeight + "px";

);



// this is the other js for the search



$(function() 
var searchTerm, panelContainerId;
// Create a new contains that is case insensitive
$.expr[":"].containsCaseInsensitive = function(n, i, m)
return (
jQuery(n)
.text()
.toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0
);
;

$("#accordion_search_bar").on("change keyup paste click", function()
searchTerm = $(this).val();
$("#accordions > .AccordionContainer").each(function()
panelContainerId = "#" + $(this).attr("id");
$(
panelContainerId + ":not(:containsCaseInsensitive(" + searchTerm + "))"
).hide();
$(
panelContainerId + ":containsCaseInsensitive(" + searchTerm + ")"
).show();
);
);
);


Basically i want to the search box to search through the buttons of all accordions and also search in every panel that i created for each accordion.










share|improve this question
















I have a list of accordions in my html page and i would like to have a search box for filtering the accordions and its contents. Basically i would like to create a search box for all the accordions but i also want to search for the contents inside the accordions. Sorry if my question is not clear but this is my first question on Stackoverflow.



Below the code is for how i am creating the accordions and how i show them when the user press one title. (i have created one accordion in the html and i clone it whenever i want to create more in the javascript file.)



// this is in the html



<input type="search" id="accordion_search_bar" placeholder="Search"/>

<div id="accordions">
<div id="accID1" class="AccordionContainer">
<button id="accID" class="accordion"></button>
<div class="panel" id="panel1">
</div>


//this is the js file



for (a = 0; a < acc.length; a++) 
acc[a].addEventListener("click", function()
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight)
panel.style.maxHeight = null;
else
panel.style.maxHeight = panel.scrollHeight + "px";

);



// this is the other js for the search



$(function() 
var searchTerm, panelContainerId;
// Create a new contains that is case insensitive
$.expr[":"].containsCaseInsensitive = function(n, i, m)
return (
jQuery(n)
.text()
.toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0
);
;

$("#accordion_search_bar").on("change keyup paste click", function()
searchTerm = $(this).val();
$("#accordions > .AccordionContainer").each(function()
panelContainerId = "#" + $(this).attr("id");
$(
panelContainerId + ":not(:containsCaseInsensitive(" + searchTerm + "))"
).hide();
$(
panelContainerId + ":containsCaseInsensitive(" + searchTerm + ")"
).show();
);
);
);


Basically i want to the search box to search through the buttons of all accordions and also search in every panel that i created for each accordion.







javascript jquery html css






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 16:43







Dts742

















asked Mar 24 at 14:23









Dts742Dts742

134




134












  • Can you please provide some HTML with contents and the searchbox so that we can see better what you want to achieve? Moreover this is not jQuery what you are using here.

    – Gutelaunetyp
    Mar 24 at 15:20












  • I edit and put the search box and the jquery code that i am using for the search. My problem is that the code isnt working as expected when i type something in the search box nothing happens

    – Dts742
    Mar 24 at 16:45


















  • Can you please provide some HTML with contents and the searchbox so that we can see better what you want to achieve? Moreover this is not jQuery what you are using here.

    – Gutelaunetyp
    Mar 24 at 15:20












  • I edit and put the search box and the jquery code that i am using for the search. My problem is that the code isnt working as expected when i type something in the search box nothing happens

    – Dts742
    Mar 24 at 16:45

















Can you please provide some HTML with contents and the searchbox so that we can see better what you want to achieve? Moreover this is not jQuery what you are using here.

– Gutelaunetyp
Mar 24 at 15:20






Can you please provide some HTML with contents and the searchbox so that we can see better what you want to achieve? Moreover this is not jQuery what you are using here.

– Gutelaunetyp
Mar 24 at 15:20














I edit and put the search box and the jquery code that i am using for the search. My problem is that the code isnt working as expected when i type something in the search box nothing happens

– Dts742
Mar 24 at 16:45






I edit and put the search box and the jquery code that i am using for the search. My problem is that the code isnt working as expected when i type something in the search box nothing happens

– Dts742
Mar 24 at 16:45













1 Answer
1






active

oldest

votes


















0














Using the HTML property innerText we can extract the text content of each accordion and check whether it contains the text for which you are searching. If it does contain the text then we show the accordion, otherwise we hide it. MDN has a great article on innerText. They state,




It approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard




You might use innerText when searching your accordions in the following way: (your accordion script uses vanilla JavaScript whereas your search is using JQuery. I'll use plain JavaScript below to ensure compatibility.)



Get a list of accordions:



accordions = document.querySelectorAll( '.AccordionContainer' );


Assuming your search is in a variable called searchText, loop through each accordion and look at its text content:



Array.prototype.forEach.call( accordions, function( accordion ) 
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
// If the index of searchText is -1 then it is not in the accordion.
// Otherwise it is, so we display the accordion
accordion.style.display = 'block';

else
// We hide the accordion if searchText is not found
accordion.style.display = 'none';

);


I converted both the search and accordion text content to lowercase for case insensitivity.



A complete example that shows an input event listener added to the search bar might look like the following:






var search = document.getElementById( 'accordion_search_bar' ),
accordions = document.querySelectorAll( '.AccordionContainer' );

// Show content on click
Array.prototype.forEach.call( accordions, function( accordion )
accordion.querySelector( 'button' ).addEventListener( 'click', function()
this.nextElementSibling.classList.add( 'active' );
);
);

// Apply search
search.addEventListener( 'input', function()
var searchText = search.value.toLowerCase();
Array.prototype.forEach.call( accordions, function( accordion )
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
accordion.style.display = 'block';

else
accordion.style.display = 'none';

);
);

.panel 
max-height: 0;
overflow: hidden;
transition: max-height 0.3s;

.panel.active
max-height: 300px;

<input type="text" id="accordion_search_bar">
<div id="accordions">
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is accordion text</div>
</div>
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is another lot of accordion text</div>
</div>
</div>





Note that using innerText will search all text content of the accordion, including the button text. If you just want to search the panel text then get that element and use innerText on that.






share|improve this answer




















  • 1





    Thank you very much it's working just like I expected!

    – Dts742
    Mar 25 at 14:44











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324783%2flive-search-for-accordions-and-its-content%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Using the HTML property innerText we can extract the text content of each accordion and check whether it contains the text for which you are searching. If it does contain the text then we show the accordion, otherwise we hide it. MDN has a great article on innerText. They state,




It approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard




You might use innerText when searching your accordions in the following way: (your accordion script uses vanilla JavaScript whereas your search is using JQuery. I'll use plain JavaScript below to ensure compatibility.)



Get a list of accordions:



accordions = document.querySelectorAll( '.AccordionContainer' );


Assuming your search is in a variable called searchText, loop through each accordion and look at its text content:



Array.prototype.forEach.call( accordions, function( accordion ) 
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
// If the index of searchText is -1 then it is not in the accordion.
// Otherwise it is, so we display the accordion
accordion.style.display = 'block';

else
// We hide the accordion if searchText is not found
accordion.style.display = 'none';

);


I converted both the search and accordion text content to lowercase for case insensitivity.



A complete example that shows an input event listener added to the search bar might look like the following:






var search = document.getElementById( 'accordion_search_bar' ),
accordions = document.querySelectorAll( '.AccordionContainer' );

// Show content on click
Array.prototype.forEach.call( accordions, function( accordion )
accordion.querySelector( 'button' ).addEventListener( 'click', function()
this.nextElementSibling.classList.add( 'active' );
);
);

// Apply search
search.addEventListener( 'input', function()
var searchText = search.value.toLowerCase();
Array.prototype.forEach.call( accordions, function( accordion )
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
accordion.style.display = 'block';

else
accordion.style.display = 'none';

);
);

.panel 
max-height: 0;
overflow: hidden;
transition: max-height 0.3s;

.panel.active
max-height: 300px;

<input type="text" id="accordion_search_bar">
<div id="accordions">
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is accordion text</div>
</div>
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is another lot of accordion text</div>
</div>
</div>





Note that using innerText will search all text content of the accordion, including the button text. If you just want to search the panel text then get that element and use innerText on that.






share|improve this answer




















  • 1





    Thank you very much it's working just like I expected!

    – Dts742
    Mar 25 at 14:44















0














Using the HTML property innerText we can extract the text content of each accordion and check whether it contains the text for which you are searching. If it does contain the text then we show the accordion, otherwise we hide it. MDN has a great article on innerText. They state,




It approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard




You might use innerText when searching your accordions in the following way: (your accordion script uses vanilla JavaScript whereas your search is using JQuery. I'll use plain JavaScript below to ensure compatibility.)



Get a list of accordions:



accordions = document.querySelectorAll( '.AccordionContainer' );


Assuming your search is in a variable called searchText, loop through each accordion and look at its text content:



Array.prototype.forEach.call( accordions, function( accordion ) 
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
// If the index of searchText is -1 then it is not in the accordion.
// Otherwise it is, so we display the accordion
accordion.style.display = 'block';

else
// We hide the accordion if searchText is not found
accordion.style.display = 'none';

);


I converted both the search and accordion text content to lowercase for case insensitivity.



A complete example that shows an input event listener added to the search bar might look like the following:






var search = document.getElementById( 'accordion_search_bar' ),
accordions = document.querySelectorAll( '.AccordionContainer' );

// Show content on click
Array.prototype.forEach.call( accordions, function( accordion )
accordion.querySelector( 'button' ).addEventListener( 'click', function()
this.nextElementSibling.classList.add( 'active' );
);
);

// Apply search
search.addEventListener( 'input', function()
var searchText = search.value.toLowerCase();
Array.prototype.forEach.call( accordions, function( accordion )
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
accordion.style.display = 'block';

else
accordion.style.display = 'none';

);
);

.panel 
max-height: 0;
overflow: hidden;
transition: max-height 0.3s;

.panel.active
max-height: 300px;

<input type="text" id="accordion_search_bar">
<div id="accordions">
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is accordion text</div>
</div>
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is another lot of accordion text</div>
</div>
</div>





Note that using innerText will search all text content of the accordion, including the button text. If you just want to search the panel text then get that element and use innerText on that.






share|improve this answer




















  • 1





    Thank you very much it's working just like I expected!

    – Dts742
    Mar 25 at 14:44













0












0








0







Using the HTML property innerText we can extract the text content of each accordion and check whether it contains the text for which you are searching. If it does contain the text then we show the accordion, otherwise we hide it. MDN has a great article on innerText. They state,




It approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard




You might use innerText when searching your accordions in the following way: (your accordion script uses vanilla JavaScript whereas your search is using JQuery. I'll use plain JavaScript below to ensure compatibility.)



Get a list of accordions:



accordions = document.querySelectorAll( '.AccordionContainer' );


Assuming your search is in a variable called searchText, loop through each accordion and look at its text content:



Array.prototype.forEach.call( accordions, function( accordion ) 
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
// If the index of searchText is -1 then it is not in the accordion.
// Otherwise it is, so we display the accordion
accordion.style.display = 'block';

else
// We hide the accordion if searchText is not found
accordion.style.display = 'none';

);


I converted both the search and accordion text content to lowercase for case insensitivity.



A complete example that shows an input event listener added to the search bar might look like the following:






var search = document.getElementById( 'accordion_search_bar' ),
accordions = document.querySelectorAll( '.AccordionContainer' );

// Show content on click
Array.prototype.forEach.call( accordions, function( accordion )
accordion.querySelector( 'button' ).addEventListener( 'click', function()
this.nextElementSibling.classList.add( 'active' );
);
);

// Apply search
search.addEventListener( 'input', function()
var searchText = search.value.toLowerCase();
Array.prototype.forEach.call( accordions, function( accordion )
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
accordion.style.display = 'block';

else
accordion.style.display = 'none';

);
);

.panel 
max-height: 0;
overflow: hidden;
transition: max-height 0.3s;

.panel.active
max-height: 300px;

<input type="text" id="accordion_search_bar">
<div id="accordions">
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is accordion text</div>
</div>
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is another lot of accordion text</div>
</div>
</div>





Note that using innerText will search all text content of the accordion, including the button text. If you just want to search the panel text then get that element and use innerText on that.






share|improve this answer















Using the HTML property innerText we can extract the text content of each accordion and check whether it contains the text for which you are searching. If it does contain the text then we show the accordion, otherwise we hide it. MDN has a great article on innerText. They state,




It approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard




You might use innerText when searching your accordions in the following way: (your accordion script uses vanilla JavaScript whereas your search is using JQuery. I'll use plain JavaScript below to ensure compatibility.)



Get a list of accordions:



accordions = document.querySelectorAll( '.AccordionContainer' );


Assuming your search is in a variable called searchText, loop through each accordion and look at its text content:



Array.prototype.forEach.call( accordions, function( accordion ) 
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
// If the index of searchText is -1 then it is not in the accordion.
// Otherwise it is, so we display the accordion
accordion.style.display = 'block';

else
// We hide the accordion if searchText is not found
accordion.style.display = 'none';

);


I converted both the search and accordion text content to lowercase for case insensitivity.



A complete example that shows an input event listener added to the search bar might look like the following:






var search = document.getElementById( 'accordion_search_bar' ),
accordions = document.querySelectorAll( '.AccordionContainer' );

// Show content on click
Array.prototype.forEach.call( accordions, function( accordion )
accordion.querySelector( 'button' ).addEventListener( 'click', function()
this.nextElementSibling.classList.add( 'active' );
);
);

// Apply search
search.addEventListener( 'input', function()
var searchText = search.value.toLowerCase();
Array.prototype.forEach.call( accordions, function( accordion )
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
accordion.style.display = 'block';

else
accordion.style.display = 'none';

);
);

.panel 
max-height: 0;
overflow: hidden;
transition: max-height 0.3s;

.panel.active
max-height: 300px;

<input type="text" id="accordion_search_bar">
<div id="accordions">
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is accordion text</div>
</div>
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is another lot of accordion text</div>
</div>
</div>





Note that using innerText will search all text content of the accordion, including the button text. If you just want to search the panel text then get that element and use innerText on that.






var search = document.getElementById( 'accordion_search_bar' ),
accordions = document.querySelectorAll( '.AccordionContainer' );

// Show content on click
Array.prototype.forEach.call( accordions, function( accordion )
accordion.querySelector( 'button' ).addEventListener( 'click', function()
this.nextElementSibling.classList.add( 'active' );
);
);

// Apply search
search.addEventListener( 'input', function()
var searchText = search.value.toLowerCase();
Array.prototype.forEach.call( accordions, function( accordion )
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
accordion.style.display = 'block';

else
accordion.style.display = 'none';

);
);

.panel 
max-height: 0;
overflow: hidden;
transition: max-height 0.3s;

.panel.active
max-height: 300px;

<input type="text" id="accordion_search_bar">
<div id="accordions">
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is accordion text</div>
</div>
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is another lot of accordion text</div>
</div>
</div>





var search = document.getElementById( 'accordion_search_bar' ),
accordions = document.querySelectorAll( '.AccordionContainer' );

// Show content on click
Array.prototype.forEach.call( accordions, function( accordion )
accordion.querySelector( 'button' ).addEventListener( 'click', function()
this.nextElementSibling.classList.add( 'active' );
);
);

// Apply search
search.addEventListener( 'input', function()
var searchText = search.value.toLowerCase();
Array.prototype.forEach.call( accordions, function( accordion )
if ( accordion.innerText.toLowerCase().indexOf( searchText ) >= 0 )
accordion.style.display = 'block';

else
accordion.style.display = 'none';

);
);

.panel 
max-height: 0;
overflow: hidden;
transition: max-height 0.3s;

.panel.active
max-height: 300px;

<input type="text" id="accordion_search_bar">
<div id="accordions">
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is accordion text</div>
</div>
<div class="AccordionContainer">
<button class="accordion">Show Content</button>
<div class="panel" id="panel1"> This is another lot of accordion text</div>
</div>
</div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 24 at 23:01

























answered Mar 24 at 22:52









jlajla

2,15021322




2,15021322







  • 1





    Thank you very much it's working just like I expected!

    – Dts742
    Mar 25 at 14:44












  • 1





    Thank you very much it's working just like I expected!

    – Dts742
    Mar 25 at 14:44







1




1





Thank you very much it's working just like I expected!

– Dts742
Mar 25 at 14:44





Thank you very much it's working just like I expected!

– Dts742
Mar 25 at 14:44



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324783%2flive-search-for-accordions-and-its-content%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해