div style.height and style.width not preserved - javascript

I have Canvas JS charts as divs with chart data in separate javascript file.
The charts are hidden by default. Only on clicking an OnClick event handler the charts need to be shown. I am using style.display='none' by default and OnClick I am setting style.display='block'. But it is not keeping the div style.height and style.width parameters fixed. The OnClick chart shown is small in size and I want it to be of the size printed in the div. How to achieve that ?
I use the code :
var x = document.getElementById(res);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
Clicking the on click event handler the charts supposed to be visible.
var str1= "chartContainer";
var res = str1.concat(dt);
var x = document.getElementById(res);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
The chart that is shown is small in size. Like :
https://www.photobox.co.uk/my/photo/full?photo_id=501983526299
But it is supposed to preserve the div height and width , like :
https://www.photobox.co.uk/my/photo/full?photo_id=501983527221

Related

Hide clicked button after toggle

I'm using this code to toggle the visibility of a section. When the button is clicked, the section shows.
function myFunction() {
var x = document.getElementById("offerte-toggle");
if (x.style.display === "block") {
x.style.display = "none";
} else {
x.style.display = "block";
}
}
<a onclick="myFunction()" id="knop-doorz" class="witte-randen">Offerte opvragen</a>
How can I hide the button after it has been clicked?
your JS code is targetting an element with id offerte-toggle but in your html, the has an id of knop-doorz
Either edit the html id to match the JS or vice versa.
You also don't need to do the if check, just simply set x.style.display = 'none'; before the closing } tag.

How can I have infinite clicks on my button because after i click the button it does not work anymore

<script type="text/javascript">
//this will make it appear
function showPicture() {
var sourceOfPicture = "img/tierlist.jpg";
var img = document.getElementById('tierlist')
img.src = sourceOfPicture.replace('90x90', '225x225');
img.style.display = "block";
}
// this will remove the picture
function removePicture() {
var image_x = document.getElementById('tierlist');
image_x.parentNode.removeChild(image_x);
img.style.display = "block";
}
</script>
I want it to have an infinite amount of clicks and not just a one and done button, how do I do it?
Your button won't show the image again after you hide it was because of this line
image_x.parentNode.removeChild(image_x);
You are removing the element out of the page completely so when you select it again with
var img = document.getElementById('tierlist')
it won't be able to find the item.
Suggestion: Setting the display style of the item to "none" when you want to hide it and set it to "block" when you want to display it.
Example:
function toggle(){
var txtDiv = document.getElementById('tierlist');
if(txtDiv.style.display == "none"){
txtDiv.style.display = "block"
} else{
txtDiv.style.display = "none"
}
}
<div id="tierlist">Hello</div>
<button onclick="toggle()">Show/hide</button>

Why does my script only act on the first element with a particular ID value?

I have a query that produces a number of DIV id="toggle_panel" I know I can effectively change the ID of the DIV dynamically.
Below is the script, straight from w3schools, which works great out of the box...for the first DIV and first DIV only. How do I apply a dynamic variable from the query to my script?
Second question: How do I get it so the DIV are hidden by default?
function myFunction() {
var x = document.getElementById("toggle_panel");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
Thank you
For your first question, using a class would be more appropriate to tag a collection similar html elements.
So your divs should look something like this:
<div class="toggle_panel">...</div>
<div class="toggle_panel">...</div>
<div class="toggle_panel">...</div>
You could then use document.getElementsByClassName('toggle_panel') to access them.
Also to hide them by default, you could use css to target your classes as shown below.
.toggle_panel {
display: none;
}
As mentioned in the comments, you can only have one instance of an ID. To achieve the result you want you will have to change the <div id="toggle_panel">content</div> to <div class="toggle_panel">content</div>. Then use the following javascript:
function myFunction() {
var panels = document.getElementsByClassName("toggle_panel");
for(var i = 0; i < panels.length; i++) {
var panel = panels[i];
if (panel.style.display === "none") {
panel.style.display = "block";
} else {
panel.style.display = "none";
}
}
}
I think you should use querySelectorAll('toggle_panel') and your code will be like this:
Array.from(document.querySelectorAll('toggle_panel')).forEach((element) => {
element.display = 'none'
})

make div appear when var is bigger than 14. otherwise keep it hidden

<script>var div = document.getElementById('demo');
if (totaal > 14) {
div.style.display = 'block';
div.style.visibility='visible';
}
else {
div.style.display = 'none';
div.style.visibility='hidden';
}</script>
when using this script the div stays hidden and doesnt become bigger. Anyone know what i can do to make the div appear when var 'totaal' is bigger than 14?
Thanks
Edit1: var totaal is already set earlier in the script. Its a large script with adding and subtracting 1 from 'totaal'. That part isn't neccesary so I didn't include it.
You can use function to toggle your div based on the value of totaal :
function toggleDiv(totaal) {
var div = document.getElementById('demo');
if (totaal > 14) {
div.style.display = 'block';
} else {
div.style.display = 'none';
}
}
Now, all the time you change tootal, you can call this function with totaal as parameter to toggle the div

Simple hide/show div+js : how to start hidden?

I have a Hide/show div with a button and javascript code. I want to start it hidden (and not show).
can you help me ?
HTML
Click Me
<div id="myDIV">
This is my DIV element.
</div>
JAVASCRIPT
function myFunction() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
this is were i have found the code : hide/show
put onload in body
<body onload="myFunction()">

Categories