HTML - Hiding objects based on class - html

I have a function that hides a div with the corresponding ID based on a radio button change, however, I would like to hide multiple items at once and as ID is unique I am not able to just hide them all. How would I set up a class that I can hide and how would I adjust this code below to make that work?
Any help greatly appreciated
function onChangePackage() {
const nodes = document.getElementsByClassName("baseClass");
var selectedValue;
// Get selected radio
for (var i = 0, length = nodes.length; i < length; i++) {
if (nodes[i].checked) {
selectedValue = nodes[i].value;
break;
}
}
// Showing all nodes first
const nodePostFix = ['A','B','C'];
nodePostFix.forEach( node => {
const currentElement = elementsToHide.item(i);
if (currentElement.hasClass("hidden" + selectedValue)) {
currentElement.style.display = "none";
} else {
currentElement.style.display = "block";
}
});
};

You can use data attributes for this purpose together with the attribute selectors. So you need just to add the data-hidden-for attributes to the required nodes and access them using document.querySelector() or document.querySelectorAll()

First give all the elements a base class name baseClass. You could just give them a class name like hidden and then in your code you could do something like below:
const elementsToHide = document.getElementsByClassName("baseClass");
for (var i = 0; i < elementsToHide.length; i++) {
const currentElement = elementsToHide.item(i);
if (currentElement.hasClass("hidden")) {
currentElement.style.display = "none";
} else {
currentElement.style.display = "block";
}
}
And on the click event of the radio button you could add this class I mentioned above to whichever ones you want to hide:
element.classList.add("hidden");
or
element.classList.remove("hidden");

Related

Reorder the context menu and add dividers in Forge Viewer

Is there a way to rearrange the order of the context menu items and possibly add dividers between them for grouping?
I followed this post https://forge.autodesk.com/blog/customize-viewer-context-menu to make a custom context menu.
For dividers I found functionality in the source code:
menu.push({divider: true})
Snippet of the source code for show() in contextMenu:
for (var i = 0; i < menu.length; ++i) {
var defn = menu[i],
title = defn.title,
target = defn.target,
icon = defn.icon,
shortcut = defn.shortcut,
divider = defn.divider;
var hasChildren = Array.isArray(target);
if (hasChildren && !menu.isChild) {
addExpandDiv = true; // Keep track of any item that is expandable
} else {
// As described in the design, limit the the number of menu levels to two.
// We will flatten the target array
hasChildren = false;
}
if (divider) {
menuItem = this.createDivider();
menuDiv.appendChild(menuItem);
} else {

HTML 5 make Checkbox required

I have multiple checkbox on my form. I need to make user to select atleast one checkbox or else the form will not submit. I tried required attribute but it checks if user has checked all check boxes.
How to do this?
One solution is to add required attributes to all the checkboxes, and when at least one of these checkboxes is checked, remove the required attributes for all the checkboxes using jQuery.
var requiredCheckboxes = $(':checkbox[required]');
requiredCheckboxes.change(function(){
if(requiredCheckboxes.is(':checked')) {
requiredCheckboxes.removeAttr('required');
}
else {
requiredCheckboxes.attr('required', 'required');
}
});
DEMO
You can use Javascript that just loops through your html checkboxes and sets the Variable issomethingchecked to TRUE if at least one of them is cheched .
demo http://jsfiddle.net/updpxrfj/2/
var checkboxElements = document.getElementsByTagName("input");
var issomethingchecked = false;
for(var i = 0; i < checkboxElements.length; i++) {
if(checkboxElements[i].type.toLowerCase() == 'checkbox') {
if (checkboxElements[i].checked == true) {
issomethingchecked = true;
}
}
if (issomethingchecked === true) {
// Continue with submiting the form
console.log(issomethingchecked);
}
}

GAS is it possible to replace getActiveDocument().getSelection() at once?

My User has the following selection in his Gdoc.
Now from the sidebar he wants to to replace the selection he made on the document. The GAS question is if it is possible to do that at once, something like:
var selection = DocumentApp.getActiveDocument().getSelection()
selection.replace("newtext")
Or do I have to loop through selection.getRangeElements() in order to delete them (or replace them) and than in someway place the new text in that position?
Not, that's not possible (well, if it is, it's not documented).
You have to loop through the selected elements, mainly because the selection may take part of paragraphs, forcing you to manage that. i.e. deleting just the selected part. And for completed selected elements, you can just remove them entirely (like images).
Here's an implementation on how to do this (part of the Kaylan's Translate script modified by me to properly replace images and partially selected paragraphs.
function replaceSelection(newText) {
var selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
var elements = selection.getRangeElements();
var replace = true;
for (var i = 0; i < elements.length; i++) {
if (elements[i].isPartial()) {
var element = elements[i].getElement().asText();
var startIndex = elements[i].getStartOffset();
var endIndex = elements[i].getEndOffsetInclusive();
var text = element.getText().substring(startIndex, endIndex + 1);
element.deleteText(startIndex, endIndex);
if( replace ) {
element.insertText(startIndex, newText);
replace = false;
}
} else {
var element = elements[i].getElement();
if( replace && element.editAsText ) {
element.clear().asText().setText(newText);
replace = false;
} else {
if( replace && i === elements.length -1 ) {
var parent = element.getParent();
parent[parent.insertText ? 'insertText' : 'insertParagraph'](parent.getChildIndex(element), newText);
replace = false; //not really necessary since it's the last one
}
element.removeFromParent();
}
}
}
} else
throw "Hey, select something so I can replace!";
}

How do I create menu in html with 4 tab which will display content on one single page?

I need to create one single web page with 4 tab. By pressing on a tab the relevant content should display on the whole page and that should work for all tabs. I mean, when user press on one tab it will display content which will be linked to that tab and hide another tab's content. How can I do it in HTML and CSS?
Jquery UI has an out of the box solution for this:
http://jqueryui.com/demos/tabs/
Granted it's not a pure HTML and CSS solution however I'm not sure you'll be able to do it without some form of scripting
This is my final solution to my problem:
function show(showEl) {
var elements = new Array("tahsilatlarIcerik", "faturaTabGraf", "odemeIcerik", "kasaIcerik");
for (var i = 0; i < elements.length; i++) {
if (showEl == elements[i]) {
document.getElementById(showEl).style.display = "block";
}
else {
hide(elements[i]);
}
}
}
function hide(hideEl) {
if (document.getElementById(hideEl).style.display != "") {
document.getElementById(hideEl).style.display = "none";
}
}
Basically, I've created an Array with elements id's inside and manipulate them with 2 function. Functions are called inside of tabs, e.g., <a id="faturaMenu" onclick="show('faturaTabGraf')">Fatura Analizi</a>. I hope this will be helpfull to someone.
I solwed problem with javascript code. I am posting it and I am wondering if there is another,
less coded way to do it? As far as I researched, I've found that in javascript declaring and asigning global variables at the same time is not supported, do you know any other simple way to do it?
AND CODE:
function getFatura() {
var tahsilatId = document.getElementById("tahsilatlarIcerik");
var faturaTabloId = document.getElementById("faturaTabloIcerik");
var faturaGrafikId = document.getElementById("faturaGrafikIcerik");
var odemelerId = document.getElementById("odemeIcerik");
var kasaId = document.getElementById("kasaIcerik");
faturaTabloId.style.display = "block";
faturaGrafikId.style.display = "block";
tahsilatId.style.display = "none";
odemelerId.style.display = "none";
kasaId.style.display = "none";
}
function getTahsilatlar() {
var tahsilatId = document.getElementById("tahsilatlarIcerik");
var faturaTabloId = document.getElementById("faturaTabloIcerik");
var faturaGrafikId = document.getElementById("faturaGrafikIcerik");
var odemelerId = document.getElementById("odemeIcerik");
var kasaId = document.getElementById("kasaIcerik");
faturaTabloId.style.display = "none";
faturaGrafikId.style.display = "none";
tahsilatId.style.display = "block";
odemelerId.style.display = "none";
kasaId.style.display = "none";
}
function getOdemeler() {
var tahsilatId = document.getElementById("tahsilatlarIcerik");
var faturaTabloId = document.getElementById("faturaTabloIcerik");
var faturaGrafikId = document.getElementById("faturaGrafikIcerik");
var odemelerId = document.getElementById("odemeIcerik");
var kasaId = document.getElementById("kasaIcerik");
faturaTabloId.style.display = "none";
faturaGrafikId.style.display = "none";
tahsilatId.style.display = "none";
odemelerId.style.display = "block";
kasaId.style.display = "none";
}
function getKasa() {
var tahsilatId = document.getElementById("tahsilatlarIcerik");
var faturaTabloId = document.getElementById("faturaTabloIcerik");
var faturaGrafikId = document.getElementById("faturaGrafikIcerik");
var odemelerId = document.getElementById("odemeIcerik");
var kasaId = document.getElementById("kasaIcerik");
faturaTabloId.style.display = "none";
faturaGrafikId.style.display = "none";
tahsilatId.style.display = "none";
odemelerId.style.display = "none";
kasaId.style.display = "block";
}
AND I AM CALLING THESE FUNCTION IN a element WITH onclick event. Basically I am changing display propertie of selected tab to BLOCK and display properties of other tabs to NONE.
Now, I have 4 tab, but I can not imagine what crowd of code will I need if it exceed to, for example 10 tabs.
Thanks,
You could do it with CSS/javascript by changing the z-indexes of the pages (as divs) onclick. This would bring one in front of the other.
Just have a div containing all the tabs, and then divs with different ids for each one.
Start with the page divs all having z-index:0;, except for the home div, which hasz-index:1;.
Each tab has an onclick="nextpage('page1') (Change page1 to the id of the corresponding page).
Then in the javascript, place this code:
function nextpage(pageid) {
//Find the highest z-index. May need to be customized
var elements = document.getElementsByTagName("*");
var highest_index = 0;
for (var i = 0; i < elements.length - 1; i++) {
if (parseInt(elements[i].style.zIndex) > highest_index) {
highest_index = parseInt(elements[i].style.zIndex;
}
}
var highest_index_1 = highest_index + 1;
//set the z index of the tab-page to the highest+1
getElementByid(pageid).object.style.zIndex="1";
}

show and hiding order list

String +
if user click the + sign i want to show some oder list
then String -
If the user click the - sign i want to hide order list
How to acheive this with javascript , not using ajax , jquery
You will have to create an id for the ordered list, e.g.
<ol id="superId">
</ol>
then on javascript
function displayOL(enabled) {
if (enabled) {
document.getElementById("superId").style.display = "none";
document.getElementById("minus").style.display = "block";
document.getElementById("plus").style.display = "none";
} else {
document.getElementById("superId").style.display = "block"
document.getElementById("minus").style.display = "none";
document.getElementById("plus").style.display = "show";
}
}
then on anchor tag
+
-
PS....I just did a rough implementation in no order....
try this and attached it to your any event, i.e. onclick, onmouseover, etc...:
function toggleList(elem){
var theList = document.getElementById(elem);
if(theList.style.display == "none"){
theList.style.display == "block";
}
else{
theList.style.display == "none";
}
}
This method can be used for anything you want to show/hide. Obviously, you can call the function and variable anything you like that makes sense...