jquery - Change function “document.getElementById” to “document.getElementsByClassName” & add fade in/out - JavaScript -


my knowledge of javascript incredibly limited , hope able me out!

i have code below, i'm trying make code work class rather id , add fade in/out effect on these functions. i've added class , id set try make clear possible i'm trying do.

<!doctype html> <html>   <head>     <style>           .sub1, .sub2 {         float: right;         background-color: blue;         width: 250px;         height: 250px;         display: none;         }          #sub1, #sub2 {         float: right;         background-color: red;         width: 250px;         height: 250px;         display: none;         }      </style>     <script language="javascript" type="text/javascript">     // class         function settabshidden(tabtohide)         {          document.getelementsbyclassname('.sub1').style.display = "none";          document.getelementsbyclassname('.sub2').style.display = "none";          }           function settabsvisible(tabtoshow)         {          document.getelementsbyclassname('.sub1').style.display = "none";          document.getelementsbyclassname('.sub2').style.display = "none";           document.getelementsbyclassname(tabtoshow).style.display = "block";          }      // id         function settabshidden(tabtohide)         {          document.getelementbyid('sub1').style.display = "none";          document.getelementbyid('sub2').style.display = "none";                                   $(tabtohide).fadeout("slow");         }           function settabsvisible(tabtoshow)         {          elements = document.getelementbyid('sub1').style.display = "none";          elements = document.getelementbyid('sub2').style.display = "none";           elements = document.getelementbyid(tabtoshow).style.display = "block";          $(tabtoshow).fadein("slow");          }          </script>     </head>   <body>      <div id="main">     <input type=button name=type value='class1' onclick="settabsvisible('.sub1');";>     <input type=button name=type value='class2' onclick="settabsvisible('.sub2');";>     <input type=button name=type value='id1' onclick="settabsvisible('sub1');";>     <input type=button name=type value='id2' onclick="settabsvisible('sub2');";>      <input type=button name=type value='hide' onclick="settabshidden();";>       <div class="sub1" id="sub1"></div>      <div class="sub2" id="sub2"></div>    </body> </html> 

your getelementsbyclassname won't work because don't want include period before class name (you'll notice byid doesn't include #. same idea) change sub1 or sub2 or whatever , should start working.

you have 4 functions defined have same names, isn't going work.

you'll have name them different "settabsvisiblebyclass" ....or can use 2 functions pass in parameter letting know whether it's class or id want use.


your example code uses jquery fading. if you're planning on using jquery don't need logic. can handle you:

$(".sub1, .sub2").hide()

or, if they're contained do:

$(tabtoshow).siblings().hide()

and don't have remember different class names.


if you're not planning on using jquery fading use css transitions. like:

.sub1, .sub2, .sub3 {     opacity: 0; }  .sub1.active, .sub2.active, .sub3.active {   opacity: 1;  -webkit-transition: opacity 1s linear;   -moz-transition: opacity 1s linear;   -ms-transition: opacity 1s linear;   -o-transition: opacity 1s linear;   transition: opacity 1s linear; } 

annnd

document.getelementbyid(tabtoshow).setattribute("class", "active")

of course, method means not using display: none, might important you. in case may want add event listeners transitionend know when add display: none.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -