I have a script that hides / shows form elements, and wrapped in a tab script (tab navigation). When the code is duplicated (per tab content), the hide / show function works for the first tab but breaks in each subsequent tab. Why? Help appreciated!
Example: http://geocities.com/edmurphy21/ hide / show script: - <script type="text/javascript">
-
function showHide()
-
{
-
var idx = this.id.split('-')[1];
-
var sp = document.getElementById('cont-'+idx);
-
sp.style.display = ('' == sp.style.display)? 'none':'';
-
}
-
-
function initContent()
-
{
-
// Check that necessary features are supported
-
if ( !document.getElementById
-
|| !document.getElementsByTagName
-
|| !document.body.style){
-
return;
-
}
-
// Get all the div elements we need
- var x = document.getElementById('container');
-
var divs = x.getElementsByTagName('div');
-
// Depending on classname, add an onclick or hide
-
var el, i = divs.length;
-
while ( i-- ){
-
el = divs[i];
-
if (el.className && /\bheading\b/.test(el.className)){
-
el.onclick = showHide;
-
} else if (el.className && /\bcontent\b/.test(el.className)){
-
el.style.display = 'none';
-
}
-
}
-
}
-
window.onload = initContent;
-
</script>
body code: - <body>
-
<div id="pagecontent">
-
<h2 class="tabset_label">Table of Contents</h2>
-
<ul class="tabset_tabs">
-
<li class="firstchild"><a href="#tab-1" class="preActive active">TAB-1</a></li>
-
<li><a class="preActive postActive" href="#tab-2">TAB-2</a></li>
-
<li><a class="preActive" href="#tab-3">TAB-3</a></li>
-
<li><a class="" href="#tab-4">TAB-4</a></li>
-
</ul>
-
<div id="tab-1" class="tabset_content tabset_content_active">
-
<h2 class="tabset_label">TAB-1</h2>
-
<form action="" method="post">
-
<div style="border: 1px solid #ddddff;" id="container">
-
<div class="heading" id="head-1"> First Name: 1</div>
-
<div class="content" id="cont-1">
-
<input name="" type="text">
-
</div>
-
<div class="heading" id="head-2"> Last Name: 2</div>
-
<div class="content" id="cont-2">
-
<input name="" type="text">
-
</div>
-
</div>
-
<input id="submit" name="submit" type="submit" value="submit" />
-
</form>
-
</div>
-
<div id="tab-2" class="tabset_content">
-
<h2 class="tabset_label">TAB-2</h2>
-
<form action="" method="post">
-
<div style="border: 1px solid #ddddff;" id="container">
-
<div class="heading" id="head-3"> First Name: 3</div>
-
<div class="content" id="cont-3">
-
<input name="" type="text">
-
</div>
-
<div class="heading" id="head-4"> Last Name: 4</div>
-
<div class="content" id="cont-4">
-
<input name="" type="text">
-
</div>
-
</div>
-
<input id="submit" name="submit" type="submit" value="submit" />
-
</form>
-
</div>
-
<div id="tab-3" class="tabset_content">
-
<h2 class="tabset_label">TAB-3</h2>
-
</div>
-
<div id="tab-4" class="tabset_content">
-
<h2 class="tabset_label">TAB-4</h2>
-
</div>
-
</div>
-
</body>
15 3601
Welcome to TSDN!
The id must be unique. You have two divs with the id "container".
Thank you for taking a look! However, I'm still having the same problem after creating a unique ID name. Consequentially TAB-2 now works and TAB-1 does not. Any suggestions?
Below contains the changes I made. script change: - // Get all the div elements we need
-
var x = document.getElementById('container');
-
var divs = x.getElementsByTagName('div');
- var z = document.getElementById('containerz');
-
var divs = z.getElementsByTagName('div');
body code change: - <div style="border: 1px solid #ddddff;" id="containerz">
script change: - // Get all the div elements we need
-
var x = document.getElementById('container');
-
var divs = x.getElementsByTagName('div');
- var z = document.getElementById('containerz');
-
var divs = z.getElementsByTagName('div');
Now you're overwriting the divs variable. Either use a different name or make another function and just pass the id of the container divs.
Now you're overwriting the divs variable. Either use a different name or make another function and just pass the id of the container divs.
I understand you correctly, but can you post an example of what either/or might look like? Hate to be such a n00b... I really appreciate it!
For example, you could have: - function initDiv(container)
-
{
-
var divs = x.getElementsByTagName('div');
-
// Depending on classname, add an onclick or hide
-
var el, i = divs.length;
-
while ( i-- ){
-
el = divs[i];
-
if (el.className && /\bheading\b/.test(el.className)){
-
el.onclick = showHide;
-
} else if (el.className && /\bcontent\b/.test(el.className)){
-
el.style.display = 'none';
-
}
-
}
-
}
-
then to call it: - var container = document.getElementById("container");
-
initDiv(container);
For example, you could have...
If you have time, would you happen to merge this script with the initial script I posted above? My arrangement(s) is not working for me... would need to see the complete composition to fully understand. Again, my n00bness and your help is compelling. Thanks again!
Also, is there any easier way besides writing a new function for each tab, such as just adding unique ID names to the x variable? For example:
var x = document.getElementById('container', 'containers');
note: this above example also does not work.
I made a mistake. In the example, replace x on line 3 with container: - var divs = container.getElementsByTagName('div');
Also, is there any easier way besides writing a new function for each tab, such as just adding unique ID names to the x variable? For example:
var x = document.getElementById('container', 'containers');
note: this above example also does not work.
You don't need to write a new function for each tab. That one function has to be called for each tab with the container id.
You don't need to write a new function for each tab. That one function has to be called for each tab with the container id.
Does this look right? Still not working... -
<script type="text/javascript">
-
function showHide()
-
{
-
var idx = this.id.split('-')[1];
-
var sp = document.getElementById('cont-'+idx);
-
sp.style.display = ('' == sp.style.display)? 'none':'';
-
}
-
-
function initDiv(container)
-
{
-
var divs = container.getElementsByTagName('div');
-
// Depending on classname, add an onclick or hide
-
var el, i = divs.length;
-
while ( i-- ){
-
el = divs[i];
-
if (el.className && /\bheading\b/.test(el.className)){
-
el.onclick = showHide;
-
} else if (el.className && /\bcontent\b/.test(el.className)){
-
el.style.display = 'none';
-
}
-
}
-
}
-
var container = document.getElementById("container");
-
initDiv(container);
-
-
function initContent()
-
{
-
// Check that necessary features are supported
-
if ( !document.getElementById
-
|| !document.getElementsByTagName
-
|| !document.body.style){
-
return;
-
}
-
// Get all the div elements we need
-
var x = document.getElementById('container');
-
var divs = x.getElementsByTagName('div');
-
// Depending on classname, add an onclick or hide
-
var el, i = divs.length;
-
while ( i-- ){
-
el = divs[i];
-
if (el.className && /\bheading\b/.test(el.className)){
-
el.onclick = showHide;
-
} else if (el.className && /\bcontent\b/.test(el.className)){
-
el.style.display = 'none';
-
}
-
}
-
}
-
window.onload = initContent;
-
</script>
Lines 34 to 46 have been replaced by the initDiv function. Lines 23 and 24 need to go inside the initContent function where line 34 currently is.
Thanks! Still not working however... - <script type="text/javascript">
-
function showHide()
-
{
-
var idx = this.id.split('-')[1];
-
var sp = document.getElementById('cont-'+idx);
-
sp.style.display = ('' == sp.style.display)? 'none':'';
-
}
-
-
function initDiv(container)
-
{
-
var divs = container.getElementsByTagName('div');
-
// Depending on classname, add an onclick or hide
-
var el, i = divs.length;
-
while ( i-- ){
-
el = divs[i];
-
if (el.className && /\bheading\b/.test(el.className)){
-
el.onclick = showHide;
-
} else if (el.className && /\bcontent\b/.test(el.className)){
-
el.style.display = 'none';
-
}
-
}
-
}
-
-
function initContent()
-
{
-
// Check that necessary features are supported
-
if ( !document.getElementById
-
|| !document.getElementsByTagName
-
|| !document.body.style){
-
return;
-
}
-
var container = document.getElementById("container");
-
initDiv(container);
-
}
-
window.onload = initContent;
-
</script>
In initContent(), add the following two lines: - var container2 = document.getElementById("containerz");
-
initDiv(container2);
This is for the second tab.
Working! Thank you much sir!
Glad to hear you got it working. Post again if you have more questions.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Wang, Jay |
last post by:
I try to group several rows in a table into a div and show/hide them by
click on a button somewhere with a javascript link. When clicked, the link
will toggle the style of the div section's style...
|
by: benzwt |
last post by:
I use the following function to hide a <div> named one.
function hideObject() {
if (ns4) {
document.n1.visibility = "hide";
}
else if (ie4) {
document.all.style.visibility = "hidden";
}...
|
by: dmiller23462 |
last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript
but my end result is I need two text boxes to stay hidden until a
particular option is selected....I've cobbled together the...
|
by: MOHSEN KASHANI |
last post by:
Hi,
I am trying to hide some form elements in a form by default and show/hide
depending on which radio button is clicked. This is what I have but it is
not working:
<head>
<style>
..noshow {...
|
by: jerryyang_la1 |
last post by:
I've found this script that allows be to hide/show form elements..
<script language="JavaScript"><!--
var toggle = true;
function show(object) {
if (document.layers && document.layers)...
|
by: Siv |
last post by:
Hi,
I have a form with a TabControl on it, I have 6 tabs across the top of the
page. What I'd like to do is have all but the first and second tab visible
when the app starts and then as the user...
|
by: asilverpeach |
last post by:
Hey Guys!
Found some great scripts here on this topic but have to make to changes to the code that I can't seem to figure out.
First,
In the following code clicking on the headers shows the...
|
by: toodi4 |
last post by:
I'm using a javascript that hides and unhides text based on a button
click. It works great across static fields on a form. The problem I
have is that I'm trying to hide and unhide various fields...
|
by: ali |
last post by:
Hello every one
i need you help regarding div hide and show. i am having a link like
<a href="#" onClick="expandWin()">show/hide </a>
<div id=showHide>
</div>
within div i have lots of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
| |