Link to home
Start Free TrialLog in
Avatar of Pradip Shenolkar
Pradip ShenolkarFlag for India

asked on

How to reuse div tag to display different information everytime in javascript?

html code:
<html>
<body>
<div id="container">
</body>
</html>

Open in new window


js code:
function call_me()
{
 container=document.getElementById('container');

//do something

}

Open in new window


I am calling this function on onclick listener. But everytime when I call this js function, new div tag is added to page.
The problem  : I want to hide earlier div and display only new div.
How to resolve this ?
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

First thing: You're missing the closing tag of the div. Should be:
<div id="container"></div>

Open in new window

After this, I don't see what you're adding a new div anywhere in your code.
Is this all you are executing?
You can't create new div with the same id ("container") as the existing one. If you still want to keep the old data hidden when new data is coming, then you'd better put data into child div of your "container" div, and hide old child divs when data is coming. E.g:

$('#container > div[class="child-div"]').hide();
$('#container').append('<div class="child-div">Your data in HTML here</div>");

Open in new window

Hi,
@Duy Pham: sorry, but there is no jQuery topic assigned nor any jQuery usage mentioned - therefore a good plain Javascript question and it might complicate things to introduce jQuery into this question and even if your code sample  could solve the issue, it is not on the right track.

KR
Rainer
ASKER CERTIFIED SOLUTION
Avatar of Duy Pham
Duy Pham
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pradip Shenolkar

ASKER

Thanks alot.