JavaScript - Is There A Better Way To Change Through Divs?
So I have a few divs that I'm hiding and showing whenever a you click on a link. So I'm a newbie at JavaScript and this is the best way I know how to do this.
var divElement = ''; function show(divElement) { if(divElement == 'add') { document.getElementById('uploadImages').style.display='none'; document.getElementById('addTutorial').style.display='block'; document.getElementById('editTutorial').style.display='none'; document.getElementById('tutorialsImages').style.display='none'; document.getElementById('pendingTutorial').style.display='none'; document.getElementById('deletedTutorial').style.display='none'; } else if(divElement == 'edit') { document.getElementById('uploadImages').style.display='none'; document.getElementById('addTutorial').style.display='none'; document.getElementById('editTutorial').style.display='block'; document.getElementById('pendingTutorial').style.display='none'; document.getElementById('tutorialsImages').style.display='none'; document.getElementById('deletedTutorial').style.display='none'; } else if (divElement == 'images') { document.getElementById('uploadImages').style.display='none'; document.getElementById('addTutorial').style.display='none'; document.getElementById('editTutorial').style.display='none'; document.getElementById('pendingTutorial').style.display='none'; document.getElementById('tutorialsImages').style.display='block'; document.getElementById('deletedTutorial').style.display='none'; } else if(divElement == 'pending') { document.getElementById('uploadImages').style.display='none'; document.getElementById('addTutorial').style.display='none'; document.getElementById('editTutorial').style.display='none'; document.getElementById('tutorialsImages').style.display='none'; document.getElementById('pendingTutorial').style.display='block'; document.getElementById('deletedTutorial').style.display='none'; } else if(divElement == 'upload') { document.getElementById('uploadImages').style.display='block'; document.getElementById('addTutorial').style.display='none'; document.getElementById('editTutorial').style.display='none'; document.getElementById('tutorialsImages').style.display='none'; document.getElementById('pendingTutorial').style.display='none'; document.getElementById('deletedTutorial').style.display='none'; } else if(divElement == 'delete') { document.getElementById('uploadImages').style.display='none'; document.getElementById('addTutorial').style.display='none'; document.getElementById('editTutorial').style.display='none'; document.getElementById('tutorialsImages').style.display='none'; document.getElementById('pendingTutorial').style.display='none'; document.getElementById('deletedTutorial').style.display='block'; } } That is a awful lot of code to do just a few simple things. But don't get me wrong it works... I just wondered if there is a cleaner way of doing this. Thanks, Jon W Similar Tutorialshttp://www.***.com/5.html i cant get a caption specific to each image to display under the arrows when the image changes. it is especially hard for me because i have to edit the javascript which confuses the **** out of me. it seemed so simple.... thanks for any help ps i cant start an id with a digit? it doesnt seem to cause any problems...why is it stated that this cannot or shouldnot be done? Hello, Current Live View: https://tornhq.com/AroundTheWorld/Lo...WorkingOn.html This content is going to be implemented into a form I am currently working on once completed. As you can see, not all of the countries have yet been broken down as some we're having difficulties doing so. Code: SList.getSelect = function (slist, option) { document.getElementById('scontent').innerHTML = ''; // empty option-content if (SList[slist][option]) { // if option from the last Select, add text-content, else, set dropdown list if (slist == 'scontent') document.getElementById('scontent').innerHTML = SList[slist][option]; else { var addata = '<option>- - -</option>'; for (var i = 0; i < SList[slist][option].length; i++) { addata += '<option value="' + SList[slist][option][i] + '">' + SList[slist][option][i] + '</option>'; } // cases for each dropdown list switch (slist) { case 'slist2': document.getElementById('slist2').innerHTML = txtsl2 + ' <select name="slist2" onchange="SList.getSelect(\'slist3\', this.value);">' + addata + '</select>'; document.getElementById('slist3').innerHTML = ''; break; case 'slist3': document.getElementById('slist3').innerHTML = txtsl3 + ' <select name="slist3" onchange="SList.getSelect(\'scontent\', this.value);">' + addata + '</select>'; break; } } } else { // empty the tags for select lists if (slist == 'slist2') { document.getElementById('slist2').innerHTML = ''; document.getElementById('slist3').innerHTML = ''; } else if (slist == 'slist3') { document.getElementById('slist3').innerHTML = ''; } } } I need to know how to edit the following, if there is a simple way to set the case for each one as they are not all split up into the same way, some are Districts, some states and whatnot so the new question varies. Or do I simply need to make another big list to do them all? Best Regards, Tim Reply With Quote 03-21-2013, 06:35 AM #2 Old Pedant View Profile View Forum Posts Supreme Master coder! Join Date Feb 2009 Posts 28,311 Thanks 82 Thanked 4,754 Times in 4,716 Posts Well, for starters, this is generally a really bad way to add <select>s and <option>s. Period. I would certainly toss out your entire getSelect() function and rewrite it. But I'm also not clear on what your question is. Are you asking *HOW* to implement the third level? I don't see any countries with any third level data, at this point. If that's what you are asking, one possible way: Code: SList.slist2 = { ... "United Kingdom" : { "England" : ["Staffordshire", "Derby", ... ], "Scotland" : ["Edinburgh", ... ], "Wales" : [ ... ], "Northern Ireland" : [ .... ] }, ... }; I have to ask: Why would you name that Slist.slist2 instead of Slist.countries ?? Not that it really matters, but why not make code more self documenting? Hi, I have a good toggle script that does what I want for the most part. I have a set of links. When you click one the content in that div replaces the previous content. When you load the page, the content of link one shows. However, I would like to have different content show, not related to any of the links, when the page first loads. Once you click on any of the links that copy will get replaced with the corresponding copy. I am attaching the code and a link to the page. Code: <!-- Hide/Show --> <script type="text/javascript"> window.onload=function() { var aObj=document.getElementsByTagName('a'); var i=aObj.length; while(i--) { aObj[i].count = i; aObj[i].onclick=function() {return showHide(this);}; showHide(aObj[i]); } }; function showHide(obj) { var aDiv=document.getElementById('wrapper').getElementsByTagName('div'); if (typeof showHide.counter == 'undefined' ) { showHide.counter = 0; } aDiv[showHide.counter].style.display = 'none'; aDiv[obj.count].style.display = 'block'; showHide.counter = obj.count; return false; } </script> http://www.michelleswann.com/test/index.html Thanks so much for any help! I have this simple checkbox menu http://myresearchfinder.com/dev/checktest.html If you check the cancer box a div will appear. If you check lung, another menu pops up. If you check colon nothing appears because it's behind the lung div. I am aware of z-index. But because a person may go back and forth between menus I need a way to make sure the appropriate div is always on top. Any ideas? Hi, I wondering if anyone can help: I've got rows of divs in this manner <div class="row">This is row 1 <a href="click">click here to show more</a></div> <div class="hidden row"><p> hidden text for row 1</p></div> <div class="row">This is row 2 <a href="click">click here to show more</a></div> <div class="hidden row"><p> hidden text for row 2</p></div> <div class="row">This is row 3 <a href="click">click here to show more</a></div> <div class="hidden row"><p> hidden text for row 3</p></div> <div class="row">This is row 4 <a href="click">click here to show more</a></div> <div class="hidden row"><p> hidden text for row 4</p></div> I wish to have some javascript to toggle the hidden row for its corresponding link. Please can someone help point me to an example if available. Any help will be greatly appreciated Here is the code I'm using: Code: <style type="text/css" rel="stylesheet" href=""> li#button { cursor:pointer; } </style> <script type="text/javascript"> var num = 6; // this is # of divs you have function swap(what) { for(var i = 0; i < num; i++) { document.getElementById('swap'+i).style.display = 'none'; } document.getElementById('swap'+what).style.display = 'block'; } var createStyle = document.createElement('style'); var createStyleProp = document.createTextNode('.hidden { display:none; }'); createStyle.appendChild(createStyleProp); document.getElementsByTagName('head')[0].appendChild(createStyle); </script> <body> <div id="content"> <ul id="a-menu"> <li id="button" onClick="swap(0)"><b>button1</b></li> <li id="button" onClick="swap(1)"><b>button2</b></li> <li id="button" onClick="swap(2)"><b>button3</b></li> </ul> <div id="swap0" class="block">content 1</div> <div id="swap1" class="hidden"> <ul id="submenu"> <li id="button" onClick="swap(3)">button1</li> <li id="button" onClick="swap(4)">button2</li> <li id="button" onClick="swap(5)">button3</li> <li id="button" onClick="swap(6)">button4</li> </ul> <div id="swap3" class="block">content 2</div> <div id="swap4" class="hidden">content 3</div> <div id="swap5" class="hidden">content 4</div> <div id="swap6" class="hidden">content 5</div> </div> <div id="swap2" class="hidden">content 6</div> </div> </body> </html> Is working fine for the first three buttons but the subdivs from swap1(div) are not working. Can anyone help me with this?? Hey there. I want to move a div around a page, and I do this by getting the original top and left values of a div, and then change them. But the problem is getting the original values; I checked online and thought that this would work: Code: var chart = document.getElementById(charDiv).style.top; var charl = document.getElementById(charDiv).style.left; alert('char_t:' + chart + ' char_l:' + charl + 'charDiv:' + charDiv); charDiv is passed into the function (its the id of the div I want to move). I know that charDiv has a value by looking at the values of the alert() statement, but the values of chart and charl are empty. The alert simply outputs char_t: char_l: charDiv: charOne any help would be appreciated I just got this problem when I try to add the expandable divs in my webpage. However when I click the divs on the page, I can't see the rest of my content. This is my page, does anybody know what happened with my code??!! http://www.calibredesign.com/clients..._news_new.html the divs are on the button of the page. Thanks!! I was wondering how I could redirect from one div to another div after " x " seconds on the same page.. Is this possible? So like basically: Index page: > Shows div1, after x seconds shows div2 etc.. I have five DIV tags that contain a photo and brief bio for partners of an accounting firm. I need to randomize these DIV tags (id="d1" - "d5") to display only one of the tags on each page load. Normally, I would do this with ColdFusion but the client isn't willing to purchase the language support for their hosting system. A sample of one of the DIVs is below. Code: <div id='d1'> <div class="subhead1">Shareholder Spotlight</div> <img style="float:left;margin:12px 10px 0px 0px" src="images/leonard_summer_photo.jpg"> <div class="subhead2">Leonard M. Sommer, CPA</div> <div class="subhead3">Managing Shareholder</div> <div class="body">Leonard has more than 30 years experience in providing tax, business and succession planning advice for family-owned and closely-held businesses. <a href="leonardsommer.html" style="color:#3d3d3d; text-decoration:underline">Learn more</a>.</div> </div> If someone could let me know how to do this in javascript I would appreciate it. You'll have to be very clear in your instructions as I don't know javascript very well. Thank you in advance for your response! I have 2 DIVs on a page, Div1 and Div2. I want to copy the contents from Div1 into Div2, then suppress the contents of Div1. I want to do this when the page loads. This is what I did: Code: <div id="Div1" style="color:red;">This is the text to be moved</div> <div id="Div2" style="color:green;"></div> <script type="text/javascript"> function swapIt() { document.getElementById('Div2').innerHTML = document.getElementById('Div1').innerHTML; document.getElementByID('Div1').style.display = "none"; } onload = swapIt(); </script> This, of course, does not work, but I don't know javascript well enough to figure out why. Could someone give me an example of what would work in this case? Hello guys, I am working with javascript and currently have the below code set up. This works well, however I would like to change the "select box" to a "search box"- When the user enters a search string, the matching DIV appears. If there is no matching DIV then "sorry nothing found" appears. Is this possible at all? Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta http-equiv="Content-Style-Type" content="text/css"> <meta http-equiv="Content-Script-Type" content="text/javascript"> </head> <body> <script type="text/javascript"> function show(obj) { no = obj.options[obj.selectedIndex].value; count = obj.options.length; for(i=1;i<=count;i++) document.getElementById('fleet'+i).style.display = 'none'; if(no>0) document.getElementById('fleet'+no).style.display = 'block'; } </script> <form name="myForm"> <select onchange="show(this)"> <option value="1">Please Choose</option> <option value="2">120</option> <option value="3">121</option> <option value="4">122</option> </select> </form> <div id="fleet1" style="display:block">Please Choose <br> </div> <div id="fleet2" style="display:none"> <br> <table valign="top" align="center" border="0" width="690" style="border-collapse: collapse" cellpadding="0" cellspacing="0" bgcolor="#336699"> <TR border="0" bordercolor="#FFFFFF"> <TD valign="top"> <?php include("php/fleet/includes/table.php"); ?> <?php $path = $_SERVER['DOCUMENT_ROOT']; $path .= "/php/fleet/120.php"; include_once($path); ?></tr></table> </td> </tr> </table></div> <div id="fleet3" style="display:none"> <br> <table valign="top" align="center" border="0" width="690" style="border-collapse: collapse" cellpadding="0" cellspacing="0" bgcolor="#336699"> <TR border="0" bordercolor="#FFFFFF"> <TD valign="top"> <?php include("php/fleet/includes/table.php"); ?> <?php $path = $_SERVER['DOCUMENT_ROOT']; $path .= "/php/fleet/121.php"; include_once($path); ?></tr></table> </td> </tr> </table></div> <div id="fleet4" style="display:none"> <br> <table valign="top" align="center" border="0" width="690" style="border-collapse: collapse" cellpadding="0" cellspacing="0" bgcolor="#336699"> <TR border="0" bordercolor="#FFFFFF"> <TD valign="top"> <?php include("php/fleet/includes/table.php"); ?> <?php $path = $_SERVER['DOCUMENT_ROOT']; $path .= "/php/fleet/122.php"; include_once($path); ?></tr></table> </td> </tr> </table></div> </body> </html> demo can be seen he http://www.nctfleetlist.co.uk/div.php I am using this code to control scrolling of 2 divs at the same time, but i want to be able control 3 (or more) divs at the same time.. what am i missing? JS in head: <script type="text/javascript"> function scrollDiv(controllingElement, controlledElementID) { var controlledElement = document.getElementById(controlledElementID); controlledElement.scrollTop = controllingElement.scrollTop; controlledElement.scrollLeft = controllingElement.scrollLeft; } </script> JS in body applied to parent / controlling div: <div id="master" onscroll="scrollDiv(this,'top');" > ...</div> Names of the 2 divs controlled by "master": <div id="top" >...</div> <div id="bottom" >...</div> I'm a total beginner at both CSS and Javascript. What I want to do is to swap things on mouseover, and keep the code as simple as possible both so I have a chance of understanding it and so it will work on as many browsers as possible. I've got an image swap to work as I hoped using the method at http://acmeous.blogspot.com/2008/07/...-on-mouse.html, and am hoping to swap DIVs in the same way, with the initial DIV containing text and a thumbnail, and the replacement DIV a full size image. I started with http://www.willmaster.com/library/fe...r_div_swap.php In the following, mousing over should cause initDiv to disappear and imageDiv to appear; mousing out should cause imageDiv to disappear and initDiv to appear. Instead, I see imageDiv flickering over initDiv whenever the mouse is moved within the div. What's the simplest way to correct my error? Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <style type="text/css"> .mybox { width:full; height:115px; border-color:black; border-style:solid; border-width:1px; padding:0px; } </style> <script type="text/javascript" language="JavaScript"><!-- function HideDIV(d) { document.getElementById(d).style.display = "none"; } function DisplayDIV(d) { document.getElementById(d).style.display = "block"; } --></script> <div id="imageDiv" class="mybox" style="display:none;"> large image here of same dimensions as DIV </div> <span onmouseover="HideDIV('initDiv');DisplayDIV('imageDiv')" onmouseout="HideDIV('imageDiv');DisplayDIV('initDiv')" style="cursor:pointer"> <div id="initDiv" class="mybox"> <table><tr><td>lots of text<td>thumbnail</table> </div> </span> </html> Anyone know of a better CLI base than the one I'm using? It's based off of http://sacrifizezone.blogspot.com/ ------------------------------ So, the way the CLI works is whenever it sees the input which has the matching ID of a DIV, it displays it. Example @ http://ijappy.webs.com/green You can download an early version of the soarce w/ the command "dl src"... It works great, for now, but as I add more & more commands I'm sure the INDEX file will surely begin to... be populated.. So I was wondering if anyone knew a way (using Javascript/Ajax/PHP I really don't care) to "PRINT" a files contents instead of showing a div. So when I typed in 'help' it looked up help.html and printed it's content... Is there such thing? Is this a bad concept? Please, help haha ;D Hi Experts, I have a fixed size area.called a container tag.And based on selection of no of divs the area should divide into those many no of equal divs.And is there any other techinic to divide area by multiple divs or parts. Thanks in advance Eswaraiah M I would like to think I understand the fundamentals of JavaScript, but I am beating my head with this and it seems so simple. I am trying to set the current div as visible, then set the old div as not visible, then set the new div id as a variable so that it can be used next. all it is doing is setting the new div to visible, and not setting the variable to be used. here is my code. this is the function [code] var Gp; function setVisibility(id, visibility) { document.getElementById(id).style.display = visibility; document.getElementById(Gp).style.display = 'none'; Gp = id; } [code] this is the html [code] <div id="group_thumb"><a href="http://pinklightdesign.com/gallery/content/6389/thumb2.jpg" onclick="setVisibility('img_name1', 'inline');" rel="enlargeimage" rev="targetdiv:loopholder, trigger:click"><img src="http://pinklightdesign.com/gallery/content/6389/thumb1.jpg" width=68 height=90 /></a></div> <div id="group_thumb"><a href="http://pinklightdesign.com/gallery/content/6396/thumb2.jpg" onclick="setVisibility('img_name2', 'inline');" rel="enlargeimage" rev="targetdiv:loopholder, trigger:click"><img src="http://pinklightdesign.com/gallery/content/6396/thumb1.jpg" width=68 height=90 /></a></div> <div id="group_thumb"><a href="http://pinklightdesign.com/gallery/content/6403/thumb2.jpg" onclick="setVisibility('img_name3', 'inline');" rel="enlargeimage" rev="targetdiv:loopholder, trigger:click"> <div id='img_name1' style='display:none'>new 1 image</div> <div id='img_name2' style='display:none'>new image 2</div> <div id='img_name3' style='display:none'>new 1 image 3</div> [code] Any help would be greatly appreciated. Cheers - James I have 3 divs (boxes) next to each other, I can input text via a textarea to each. I need all 3 to resize - be the same size on text overflow - so if box a gets 100 lines of code and resizes, the other 2 should follow. I have written this js, and added it to the button which moves the text to the boxes (to the onclick) Code: // JavaScript Document function adjust() { if (document.getElementById('section1').style.height != 380 + "px") { document.getElementById('section2').style.height === document.getElementById('section1').style.height; document.getElementById('section3').style.height === document.getElementById('section1').style.height; } else if (document.getElementById('section2').style.height != 380 + "px") { document.getElementById('section1').style.height === document.getElementById('section2').style.height; document.getElementById('section3').style.height === document.getElementById('section2').style.height; } else if (document.getElementById('section3').style.height != 380 + "px") { document.getElementById('section1').style.height === document.getElementById('section3').style.height; document.getElementById('section2').style.height === document.getElementById('section3').style.height; } } any ideas what's wrong here? Hey friends, I cannot seem to figure this out. I am attempting to utilize one horizontal scroll bar at the bottom of the second div that will control the scrolling of both the first and second div. The second div also already has another scroll bar on the left. If this can be done without a scroll bar even appearing on the first, it would be great. This is the CSS of the two divs I am working with: Code: #con3 { width: 740pt; height: 100pt; margin: 0; } #con4 { position: absolute; width: 740pt; height: 120pt; margin: 0; } #con3 is located directly above #con4 within another div. I love quick replyers. Joshie |