Solved

why dynamicllay id change for div tag  is not work ing properly ?

Posted on 2010-11-08
13
220 Views
Last Modified: 2012-05-10
Hi ,

I i have 2  div tag


if i click i call some java script method and  do dynamicaly change the id of the both div .
buyLV3 dynamcalliy changed but c2 is not changing .

 document.getElementById("c2").id = 'c3';
       document.getElementById("buyLV3").id = 'buyLV2';

c2,c3,buLV3,buyLV2 are there is CSS file

<div id="buyLV3" class="divider" ></div>  

<div id="c2" class="controls">  

  <input type="button" value="<" size="2" onclick="back();"> 

     <input type="button" value=">" size="2" onclick="front();">

</div>

Open in new window

0
Comment
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 14

Expert Comment

by:sam2912
Comment Utility
You might want to check if there are multiple elements with same ID.
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
Comment Utility
That only one div c2 and c3 id is not assign for any div tag ...
in java script junction only i assign
0
 
LVL 14

Expert Comment

by:sam2912
Comment Utility
From what I can see in the provided info, it should work. You might want to consider providing more information like a link to the page or upload the whole page HTML here.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Try :

document.getElementById("c2").setAttribute("id", 'c3');
document.getElementById("buyLV3").setAttribute("id", 'buyLV2');
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
Comment Utility
sam and leakim check this below code
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

 <script type="text/javascript">

 



 function back()

{

	var a= document.getElementById("divCount").value;

	alert(a);

	if(a==1)

	 {

		

	 }

	if(a==2)

	 {

		

		document.getElementById("buyLV2").setAttribute("id", 'buyLV1'); 

		document.getElementById("divCount").value = parseInt(a)-1; 

		alert( document.getElementById("divCount").value);



	 }

	 if (a==3)

	 {

		 alert("inside");

		 document.getElementById("buyLV3").setAttribute("id", 'buyLV2'); 

		 document.getElementById("divCount").value=parseInt(a)-1; 

		 alert( document.getElementById("divCount").value); 



	 }

}



function front()

{

	 document.getElementById("buyLV3").id = 'buyLV4';



}

</script>

 <style type="text/css">  

<!--  

.box {  

        position:absolute;  

        width:50px;  

        height:170px;  

        z-index:11;  

}  

.yellow {  

        background-color:#FF0;  

}  

.orange {  

        background-color:#FC6;  

}  

.green {  

        background-color:#9F9;  

}  

.blue {  

        background-color:#3CC;  

}  

#appDiv1 {  

        left:340px;  

        top:150px;  

}  

#appDiv2 {  

        left:420px;  

        top:150px;  

}  

#appDiv3 {  

        left:500px;  

        top:150px;  

}  

#appDiv4 {  

        left:580px;  

        top:150px;  

}  

#appDiv5 {  

        left:660px;  

        top:150px;  

}  

#appDiv6 {  

        left:340px;  

        top:350px;  

}  

#appDiv7 {  

        left:420px;  

        top:350px;  

}  

#appDiv8 {  

        left:500px;  

        top:350px;  

}  

#appDiv9 {  

        left:580px;  

        top:350px;  

}  

#appDiv10 {  

        left:660px;  

        top:350px;  

}  

.divider {  

        position:absolute;  

        background-color: #FF0000;  

}  

#buyLH1 {  

        left:310px;   

        top:340px;   

        width:440px;   

        height:3px;   

        z-index:22;   

        padding:1px; 

        line-height:0px; 

}  

#buyLV3 {  

        left:565px;  

        top:130px;  

        width:3px;  

        height:420px;  

        z-index:22;

        padding:1px;  

}  

#buyLV2 {  

        left:490px;  

        top:130px;  

        width:3px;  

        height:420px;  

        z-index:22;

        padding:1px;  

        margin: 0;

}



#buyLV4 {  

        left:645px;  

        top:130px;  

        width:3px;  

        height:420px;  

        z-index:22;

        padding:1px;  

} 

#buyLV1 {  

        left:410px;  

        top:130px;  

        width:3px;  

        height:420px;  

        z-index:22;

        padding:1px;  

} 

.controls {  

       position:absolute; 

        width:60px;  

        height:30px;  

        z-index:3;  

}  

#c1 {  

        left:103px;  

        top:287px;  

}  

#c2 {  

        left:545px;  

        top:555px;  

}  

 



-->  

</style> 

</head>

<body>

<form>

<fieldset>

<div id="appDiv1" class="box yellow"></div>  

				<div id="appDiv2" class="box yellow"></div>  

				<div id="appDiv3" class="box yellow"></div>  

				<div id="appDiv4" class="box green"></div>  

				<div id="appDiv5" class="box green"></div>  

				  

				<div id="appDiv6" class="box orange"></div>  

				<div id="appDiv7" class="box orange"></div>  

				<div id="appDiv8" class="box orange"></div>  

				<div id="appDiv9" class="box blue"></div>  

				<div id="appDiv10" class="box blue"></div>  

			 

			  <div id="buyLH1" class="divider">&nbsp;</div>  

				<div id="buyLV3" class="divider" ></div>  

                <div id="c1" class="controls">  

			  

				</div>  

				 <input type="hidden" id="divCount" name="divCount" value="3">

				<div id="c2" class="controls">  

				  <input type="button" value="<" size="2" onclick="back();"> 

			      <input type="button" value=">" size="2" onclick="front();">

			      

				</div> 

				</fieldset> 

</form>				

</body>

</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
front() can work only if you click on the ">" button the first time and it work only one time
back() work three time if you don't click on ">"

that's the logic of your current code. please confirm it work like that for you.
there's not problem to set id on my side
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 20

Author Comment

by:Sathish David Kumar N
Comment Utility
>>> back() work three time if you don't click on ">"
no it wont work ...
Can you copy & paste my code and save as html file and try ....
1st time its working gd ...
2 nd time i press '<' button it wont work its not set the proper Id value ....



0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
I did. Whats your browser and its version ?
0
 
LVL 6

Expert Comment

by:blueghozt
Comment Utility
it won't work the second time as the id your front() method is looking for no longer exists (you changed it with your last click).

Why not use an iterative method for front() as you do for back()?

Using the div count is the only way to allow multiple clicks....

0
 
LVL 20

Author Comment

by:Sathish David Kumar N
Comment Utility
@ leakim971: IE6
@blueghozt: How to use  iterative method ?
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
Comment Utility
Any update ??
0
 
LVL 6

Expert Comment

by:blueghozt
Comment Utility
you asked how to use the iterative method you use for back() - in simple terms you are counting which div you are showing with the divCount value and decreasing this with each click on the back link - but you are not increasing it with the clicks on the front link

 try this (you may need to tweak depending on how many divs you end up with):


<script type="text/javascript">

 



 function back()

{

	var a= document.getElementById("divCount").value;

	alert(a);

	if(a==1)

	 {

		

	 }

	if(a==2)

	 {

		

		document.getElementById("buyLV2").setAttribute("id", 'buyLV1'); 

		document.getElementById("divCount").value = parseInt(a)-1; 

		alert( document.getElementById("divCount").value);



	 }

	 if (a==3)

	 {

		 alert("inside");

		 document.getElementById("buyLV3").setAttribute("id", 'buyLV2'); 

		 document.getElementById("divCount").value=parseInt(a)-1; 

		 alert( document.getElementById("divCount").value); 



	 }

}



function front()

{

	var a= document.getElementById("divCount").value;

	alert(a);

	if(a==1)

	 {

		document.getElementById("buyLV1").setAttribute("id", 'buyLV2'); 

		document.getElementById("divCount").value = parseInt(a)+1; 

		alert( document.getElementById("divCount").value);		

	 }

	if(a==2)

	 {

		

		document.getElementById("buyLV2").setAttribute("id", 'buyLV3'); 

		document.getElementById("divCount").value = parseInt(a)+1; 

		alert( document.getElementById("divCount").value);



	 }

	 if (a==3)

	 {

		 alert("inside");

		 document.getElementById("buyLV3").setAttribute("id", 'buyLV4'); 

		 document.getElementById("divCount").value=parseInt(a)+1; 

		 alert( document.getElementById("divCount").value); 



	 }



}

</script>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
IE6...Ok.
FYI it work fine on FF, IE8, and Chrome.

You may use an additional class instead replacing the id.

The div : div id="buyLV" class="divider buyLV3"

We change the class, for example :

      if(a==2)
       {
            document.getElementById("buyLV").className = 'divider buyLV1';

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
 <script type="text/javascript">
 

 function back()
{
	var a= document.getElementById("divCount").value;
	alert(a);
	if(a==1)
	 {
		
	 }
	if(a==2)
	 {
		document.getElementById("buyLV").className = 'divider buyLV1'; 
		document.getElementById("divCount").value = parseInt(a)-1; 
		alert( document.getElementById("divCount").value);

	 }
	 if (a==3)
	 {
		 alert("inside");
		 document.getElementById("buyLV").className = 'divider buyLV2'; 
		 document.getElementById("divCount").value=parseInt(a)-1; 
		 alert( document.getElementById("divCount").value); 

	 }
}

function front()
{
	 document.getElementById("buyLV").className = 'divider buyLV4';

}
</script>
 <style type="text/css">  
<!--  
.box {  
        position:absolute;  
        width:50px;  
        height:170px;  
        z-index:11;  
}  
.yellow {  
        background-color:#FF0;  
}  
.orange {  
        background-color:#FC6;  
}  
.green {  
        background-color:#9F9;  
}  
.blue {  
        background-color:#3CC;  
}  
#appDiv1 {  
        left:340px;  
        top:150px;  
}  
#appDiv2 {  
        left:420px;  
        top:150px;  
}  
#appDiv3 {  
        left:500px;  
        top:150px;  
}  
#appDiv4 {  
        left:580px;  
        top:150px;  
}  
#appDiv5 {  
        left:660px;  
        top:150px;  
}  
#appDiv6 {  
        left:340px;  
        top:350px;  
}  
#appDiv7 {  
        left:420px;  
        top:350px;  
}  
#appDiv8 {  
        left:500px;  
        top:350px;  
}  
#appDiv9 {  
        left:580px;  
        top:350px;  
}  
#appDiv10 {  
        left:660px;  
        top:350px;  
}  
.divider {  
        position:absolute;  
        background-color: #FF0000;  
}  
#buyLH1 {  
        left:310px;   
        top:340px;   
        width:440px;   
        height:3px;   
        z-index:22;   
        padding:1px; 
        line-height:0px; 
}  
.buyLV3 {  
        left:565px;  
        top:130px;  
        width:3px;  
        height:420px;  
        z-index:22;
        padding:1px;  
}  
.buyLV2 {  
        left:490px;  
        top:130px;  
        width:3px;  
        height:420px;  
        z-index:22;
        padding:1px;  
        margin: 0;
}

.buyLV4 {  
        left:645px;  
        top:130px;  
        width:3px;  
        height:420px;  
        z-index:22;
        padding:1px;  
} 
.buyLV1 {  
        left:410px;  
        top:130px;  
        width:3px;  
        height:420px;  
        z-index:22;
        padding:1px;  
} 
.controls {  
       position:absolute; 
        width:60px;  
        height:30px;  
        z-index:3;  
}  
#c1 {  
        left:103px;  
        top:287px;  
}  
#c2 {  
        left:545px;  
        top:555px;  
}  
 

-->  
</style> 
</head>
<body>
<form>
<fieldset>
<div id="appDiv1" class="box yellow"></div>  
				<div id="appDiv2" class="box yellow"></div>  
				<div id="appDiv3" class="box yellow"></div>  
				<div id="appDiv4" class="box green"></div>  
				<div id="appDiv5" class="box green"></div>  
				  
				<div id="appDiv6" class="box orange"></div>  
				<div id="appDiv7" class="box orange"></div>  
				<div id="appDiv8" class="box orange"></div>  
				<div id="appDiv9" class="box blue"></div>  
				<div id="appDiv10" class="box blue"></div>  
			 
			  <div id="buyLH1" class="divider">&nbsp;</div>  
				<div id="buyLV" class="divider buyLV3" ></div>  
                <div id="c1" class="controls">  
			  
				</div>  
				 <input type="hidden" id="divCount" name="divCount" value="3">
				<div id="c2" class="controls">  
				  <input type="button" value="<" size="2" onclick="back();"> 
			      <input type="button" value=">" size="2" onclick="front();">
			      
				</div> 
				</fieldset> 
</form>				
</body>
</html>

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now