Javascript button to show on condition

display button based on condition
hide and show textbox using javascript
onclick show div and hide other div
how to check div is display:none or block in javascript
show form on button click html
javascript if else hide/show div
condition javascript
how to hide and show table in javascript

I am trying to make the button visible only when there's a table shown, but it doesn't seem to be working, the button is not hiding.

PS: Sorry for not clarifying, my javascript runs onload

HTML:

var DLFunc = document.getElementsByTagName("table");
var DLButtons = document.getElementById("tableToCsv");
if (DLFunc == "") {
  DLButtons.style.visibility = 'hidden';
} else if (DLFunc != "") {
  DLButtons.style.visibility = 'visible';
}
<div id="view-container">
  <main ng-view></main>
</div>
<div id="tableToCsv">
  <button class="btnCSV">CSV file</button>
</div>

Check if there is a table shown by counting the number of table tags. Instead of checking empty string, check for the length of the array:

var DLFunc = document.getElementsByTagName("table");
var DLButtons = document.getElementById("tableToCsv");
if (DLFunc.length == 0) {
  DLButtons.style.visibility = 'hidden';
} else  {
  DLButtons.style.visibility = 'visible';
}

Also check when the code runs (maybe add an alert) as the tables may not have been created at the time your JS is running.

UPDATE:

Your JS won't pick up the table as it is run on page load, whereas AngularJS probably creates the elements later.

You can add something like the following to your DLButtons HTML:

<button class="btnCSV" *ngShow="hasTable()">CSV file</button>

Then in your Angular Component.ts/js:

hasTable() {
  return document.getElementsByTagName("table").length > 0;
}

So we'll have to change the style dynamically, using JavaScript, triggered by an onclick event. First, add the element that you want the user to click: <button onclick  I'm trying to make a button appear if condition "isauthenticated" is "true". If condition is true show button. Trigger a button click with JavaScript on the

You can do the following;

// you can check directly in the condition 
 const DLFunc = document.getElementsByTagName("table");
 const DLButtons = document.getElementById("tableToCsv");
 if (DLFunc)
 {
    DLButtons.style.visibility = 'hidden';
 }
 else 
 {
    DLButtons.style.visibility = 'visible';
 }

=== 'block') { hide(elem); return; } // Otherwise, show it show(elem); }; In JavaScript we have the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true Use else to specify a block of code to be executed, if the same condition is false

Try this:

var DLFunc = document.getElementsByTagName("table");
var DLButtons = document.getElementById("tableToCsv");
if (DLFunc.length == 0) {
   DLButtons.style.visibility = 'hidden';
} else {
   DLButtons.style.visibility = 'visible';
}
<div id="view-container">
  <main ng-view></main>
</div>
<div id="tableToCsv">
  <button class="btnCSV">CSV file</button>
</div>

How do you check whether a button is clicked or not in JavaScript? Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete

How do you hide a button after it is clicked in JavaScript? The <input type="button"> defines a clickable button (mostly used with a JavaScript to activate a script). The numbers in the table specify the first browser version that fully supports the element.

javascript Display button on a HTML page based on condition. By LuluSW, August 26, 2017 in User JavaScript and CSS Discussions · html. Popup message can be shown using global functions - alert(), confirm() and prompt(). alert() function displays popup message with 'Ok' button. confirm() function display popup message with 'Ok' and 'Cancel' buttons. Use confirm() function to take user's confirmation to proceed.

Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  submitObject.disabled. The JavaScript disabled property is a boolean property that takes a true or false. Setting the property to true will enable the button (clickable) and setting it false (like bt.disabled = false;) will disable the button (un-clickable). Here’s what I am doing.

Comments
  • i suggest get the length of element visible table then you can be sure that there is a table
  • Do u mind elaborating more on that? @guradio Please and thanks
  • something like DLFunc.length > 0 but you have another requirement right? visible table? this will only check if there are tables you need to check if that table is visible or not
  • Still not working, could you help me check if my code arrangement is wrong or something? I placed the js code in DownloadCSV.js
  • are you running your js on load or by triggering an event?
  • Still not working, maybe the way i placed my code is wrong? You mind having a look for me? I updated it
  • Angular will add the table later, after your JS is run (it will only run once as the page loads)
  • It hides on default now, when i go to a page with table, it shows. But when i go to a page without table again, the button doesn't hide.
  • Your condition if (DLFunc.length > 0) { DLButtons.style.visibility = 'hidden'; } else { DLButtons.style.visibility = 'visible'; } is doing opposite of what is asked. Check my answer.
  • Did you just downvoted my post because I corrected you?
  • The button isn't showing up when I change to a view with table
  • Not working, maybe cause my js is loaded on page load, it's not working
  • @HangulSR The code is working if you add a table tag inside <main ng-view></main>. The rest is up to you how you place it in your markup or external JS. See sample here: jsfiddle.net/83L7yx5p