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;
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
add a comment |
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
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
add a comment |
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
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
javascript jquery html css
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
1
Thank you very much it's working just like I expected!
– Dts742
Mar 25 at 14:44
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
1
Thank you very much it's working just like I expected!
– Dts742
Mar 25 at 14:44
add a comment |
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.
1
Thank you very much it's working just like I expected!
– Dts742
Mar 25 at 14:44
add a comment |
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.
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>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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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