How can i make my second button have an onclick function - html

I created a button where when clicked on creates another button but Iam not sure how to make an onclick event for that button
function boyFunction(){
var btn1 = document.createElement("BUTTON");
var x = document.createTextNode("basketball");
btn1.appendChild(x);
document.body.appendChild(btn1);
btn1.classList.add('btn1');
}
I want to be able to click the basketball button and have that button show an image

Three things had to be done.
First your new element will need an id
btn1.setAttribute("id", "myButton");
click event handler will need to be created for your new element
document.getElementById("myButton").addEventListener("click", myButtonClickHandler);
and then you will define your click handler in a new function
function myButtonClickHandler {
// my code
}
Your code after doing the above change will look like below:
function boyFunction(){
var btn1 = document.createElement("BUTTON");
btn1.setAttribute("id", "myButton");
var x = document.createTextNode("basketball");
btn1.appendChild(x);
document.body.appendChild(btn1);
btn1.classList.add('btn1');
document.getElementById("myButton").addEventListener("click", myButtonClickHandler);
}
function myButtonClickHandler {
// my code
}

You can add an Click Handler like this:
document.getElementById('button').onclick = function() {
alert("button was clicked");
}​;​
of course you need to give your new button the id 'button' or any other id you choose

You can simply do this
function boyFunction(){
var btn1 = document.createElement("BUTTON");
btn1.addEventListener('click',()=>console.log('clicked'));
var x = document.createTextNode("basketball");
btn1.appendChild(x);
document.body.appendChild(btn1);
btn1.classList.add('btn1');
}

Can work like this:
function boyFunction(){
var btn1 = document.createElement("BUTTON");
// your "onclick function" goes here
btn1.onclick = function () { };
var x = document.createTextNode("basketball");
btn1.appendChild(x);
document.body.appendChild(btn1);
btn1.classList.add('btn1');
}

Related

Forge Viewer Extension for Toolbar: How to add a custom combox

I am trying to add a custom combobox to the toolbar in the forge viewer. Below is the code for it. I am able to successfully able to add buttons and they are functional. But combobox is not. It adds a combobox but it does show the fly out menu when I click on it. Not sure what I am doing wrong. help!
function BuildingToolbarExtension(viewer, options) {
Autodesk.Viewing.Extension.call(this, viewer, options);
}
BuildingToolbarExtension.prototype = Object.create(Autodesk.Viewing.Extension.prototype);
BuildingToolbarExtension.prototype.constructor = BuildingToolbarExtension;
BuildingToolbarExtension.prototype.load = function () {
// Set background environment to "Infinity Pool"
// and make sure the environment background texture is visible
this.viewer.setLightPreset(6);
this.viewer.setEnvMapBackground(true);
// Ensure the model is centered
//this.viewer.fitToView();
return true;
};
BuildingToolbarExtension.prototype.unload = function () {
// nothing yet
if (this.subToolbar) {
this.viewer.toolbar.removeControl(this.subToolbar);
this.subToolbar = null;
}
};
BuildingToolbarExtension.prototype.onToolbarCreated = function (toolbar) {
alert('TODO: customize Viewer toolbar');
var viewer = this.viewer;
// Button 1
var button1 = new Autodesk.Viewing.UI.Button('show-env-bg-button');
button1.onClick = function (e) {
viewer.setEnvMapBackground(true);
};
button1.addClass('show-env-bg-button');
button1.setToolTip('Show Environment');
// Button 2
var button2 = new Autodesk.Viewing.UI.Button('hide-env-bg-button');
button2.onClick = function (e) {
viewer.setEnvMapBackground(false);
};
button2.addClass('hide-env-bg-button');
button2.setToolTip('Hide Environment');
var comboButton = new Autodesk.Viewing.UI.ComboButton('buildings');
comboButton.setToolTip('buildings');
this.floors = new Autodesk.Viewing.UI.ControlGroup('my-custom-toolbar1');
this.floors.addControl(button1);
this.floors.addControl(button2);
comboButton.addControl(this.floors);
comboButton._isCollapsed = true;
comboButton.onClick = function (e) {
this.setCollapsed(false);
}
// SubToolbar
this.subToolbar = new Autodesk.Viewing.UI.ControlGroup('my-custom-toolbar');
this.subToolbar.addControl(button1);
this.subToolbar.addControl(button2);
this.subToolbar.addControl(comboButton);
toolbar.addControl(this.subToolbar);
};
Autodesk.Viewing.theExtensionManager.registerExtension('BuildingToolbarExtension', BuildingToolbarExtension);
The ControlGroup is unnecessary in your case, please refer the following to add buttons to ComboButton
var comboButton = new Autodesk.Viewing.UI.ComboButton('buildings');
comboButton.setToolTip('buildings');
// Button 1
var button1 = new Autodesk.Viewing.UI.Button('show-env-bg-button');
button1.onClick = function (e) {
viewer.setEnvMapBackground(true);
};
button1.addClass('show-env-bg-button');
button1.setToolTip('Show Environment');
comboButton.addControl(button1);
// Button 2
var button2 = new Autodesk.Viewing.UI.Button('hide-env-bg-button');
button2.onClick = function (e) {
viewer.setEnvMapBackground(false);
};
button2.addClass('hide-env-bg-button');
button2.setToolTip('Hide Environment');
comboButton.addControl(button2);
Here are snapshots:
Before opening
After opening

How to dynamically enable/disable a button based on a form field selection?

Lets say I have a switch as shown below,
var switchKeyValue = CardService.newKeyValue()
.setTopLabel("Switch key value widget label")
.setContent("This is a key value widget with a switch on the right")
.setSwitch(CardService.newSwitch()
.setFieldName("form_input_switch_key")
.setValue("form_input_switch_value")
.setOnChangeAction(CardService.newAction()
.setFunctionName("handleSwitchChange")));
And a button as shown below,
var textButton = CardService.newTextButton()
.setText("Open Link")
.setOpenLink(CardService.newOpenLink()
.setUrl("https://www.google.com"))
I noticed, that the Button has a method setDisabled(disabled). Can this be leveraged to disable/enable the button based on the switch selection?
Yes, it is possible, see the following example:
We need to have a function that holds and returns the card:
function createCard(val) {
var action = CardService.newAction().setFunctionName('toggleIt').setParameters({val: val.toString()});
var textButton = CardService.newTextButton()
.setText("Open Link")
.setDisabled(val)
.setOpenLink(CardService.newOpenLink()
.setUrl("https://www.google.com"));
var textButton2 = CardService.newTextButton()
.setText('Click me!')
.setOnClickAction(action);
var section = CardService.newCardSection().setHeader('Disabled/Enabled').addWidget(textButton);
var section2= CardService.newCardSection().setHeader('Switcher').addWidget(textButton2);
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader()
.setTitle('My toggle button'))
.addSection(section)
.addSection(section2)
.build();
return card;
}
As you see we pass a parameter to this function: val.
This parameter holds the state of the button, and anytime you click on the button2 it calls an action: toggleIt
toggleIt function will invert the state of the val value and update the card with updateCard
Here you have it:
function toggleIt(e) {
var boolVal = e.parameters.val == 'true' ? true : false;
var val = !boolVal;
return CardService.newNavigation().updateCard(createCard(val));
}
You can easily adapt this to your code ;)

How to get / use the name of 2 widgets and use them in a function?

I'm trying to set up a function to hide Button1 and to make Button2 visible.
So far I can hide Button1 as I've written the following code in the Script Section:
function nextButton(Button1) {
Button1.visible = false;
}
In the OnClick event of Button1 I wrote:
nextButton(widget);
What I'm trying to do is to send Button2 to the function. I tried the following:
OnClick event: nextButton2(widget, Button2);
Script: function nextButton(Button1, Button2) {
Button1.visible = false;
Button2.visible = true; }
This way when I click on Button1 it would hide and Button2 will appear.
But it seems that sending the name of the object (Button2) is not working.
Do you know how I can reference another object and send it to the function?
You should get the Button2 element from the global variable app.
var Button2 = app.pages.PageWithTheButton.Button2; //Assuming the button is directly in the page.

Embedding a button in a grid causes the grid to behave as the button?

Using Google Apps Script to build a web app, when I put a button widget in a grid widget, it seems the entire grid turns into the "button".
i.e. if I put:
var myGrid = app.createGrid(4,4);
var addButton = myGrid.setWidget(3,3,app.createButton("Add"));
var handler = app.createServerClickHandler('add');
addButton.addClickHandler(handler);
app.add(myGrid);
In the resulting web app if I click anywhere on the grid the clickhandler for the button fires. Even worse, if I embed multiple buttons in the grid, clicking anywhere on the grid fires all button clickhandlers.
Are buttons in grids not supported? Or am I doing something wrong?
Thanks.
Edit: If you want to see the behaviour for yourself, I've replicated the issue by modifying one of Google's examples. The second example for "Validators" here: https://developers.google.com/apps-script/uiapp#Validators I modified and put the textboxes and the button in a Grid Widget. After entering numbers in the text boxes, every time you click anywhere on the grid the "add" clickhandler will fire and add the numbers again:
function doGet() {
var app = UiApp.createApplication();
var rgx = "^\\$?[0-9]+$";
// Create input boxes and button.
//var textBoxA = app.createTextBox().setId('textBoxA').setName('textBoxA');
//var textBoxB = app.createTextBox().setId('textBoxB').setName('textBoxB');
var myGrid = app.createGrid(4,4);
myGrid.setWidget(0,0,app.createTextBox().setId('textBoxA').setName('textBoxA'));
myGrid.setWidget(0,1,app.createTextBox().setId('textBoxB').setName('textBoxB'));
var textBoxA = app.getElementById('textBoxA');
var textBoxB = app.getElementById('textBoxB');
var addButton = myGrid.setWidget(3,3,app.createButton("Add").setEnabled(false));
var label = app.createLabel("Please input two numbers");
// Create a handler to call the adding function.
// Two validations are added to this handler so that it will
// only invoke 'add' if both textBoxA and textBoxB contain
// numbers.
var handler = app.createServerClickHandler('add').validateMatches(textBoxA,rgx).validateMatches(textBoxBrgx).addCallbackElement(textBoxA).addCallbackElement(textBoxB);
// Create a handler to enable the button if all input is legal
var onValidInput = app.createClientHandler().validateMatches(textBoxA,rgx).validateMatches(textBoxB,rgx).forTargets(addButton).setEnabled(true).forTargets(label).setVisible(false);
// Create a handler to mark invalid input in textBoxA and disable the button
var onInvalidInput1 = app.createClientHandler().validateNotMatches(textBoxA,rgx).forTargets(addButton).setEnabled (false).forTargets(textBoxA).setStyleAttribute("color", "red").forTargets(label).setVisible(true);
// Create a handler to mark the input in textBoxA as valid
var onValidInput1 = app.createClientHandler().validateMatches(textBoxA,rgx).forTargets(textBoxA).setStyleAttribute("color", "black");
// Create a handler to mark invalid input in textBoxB and disable the button
var onInvalidInput2 = app.createClientHandler().validateNotMatches(textBoxB,rgx).forTargets(addButton).setEnabled(false).forTargets(textBoxB).setStyleAttribute("color", "red").forTargets(label).setVisible(true);
// Create a handler to mark the input in textBoxB as valid
var onValidInput2 = app.createClientHandler().validateMatches(textBoxB,rgx).forTargets(textBoxB).setStyleAttribute("color","black");
// Add all the handlers to be called when the user types in the text boxes
textBoxA.addKeyUpHandler(onInvalidInput1);
textBoxB.addKeyUpHandler(onInvalidInput2);
textBoxA.addKeyUpHandler(onValidInput1);
textBoxB.addKeyUpHandler(onValidInput2);
textBoxA.addKeyUpHandler(onValidInput);
textBoxB.addKeyUpHandler(onValidInput);
addButton.addClickHandler(handler);
app.add(myGrid);
//app.add(textBoxB);
//app.add(addButton);
app.add(label);
return app;
}
function add(e) {
var app = UiApp.getActiveApplication();
var result = parseFloat(e.parameter.textBoxA) + parseFloat(e.parameter.textBoxB);
var newResultLabel = app.createLabel("Result is: " + result);
app.add(newResultLabel);
return app;
}
When you write
var addButton = myGrid.setWidget(3,3,app.createButton("Add"));
and then you add a handler to the variable addButton you are in fact adding the handler to the grid, not to the button.
I would suggest to rewrite it like this (I commented the code) and it will work normally
var myGrid = app.createGrid(4,4);
var addButton = app.createButton("Add");
myGrid.setWidget(3,3,addButton);// here you add the button to the grid
var handler = app.createServerClickHandler('add');
addButton.addClickHandler(handler);
app.add(myGrid);// only the grid must be added, the button is already in it
or, if you want to make it more compact :
var handler = app.createServerClickHandler('add');
var myGrid = app.createGrid(4,4).setWidget(3,3,createButton("Add",handler));// here you add the button to the grid
app.add(myGrid);// only the grid must be added, the button is already in it

to avoid double click inside wrap function in html

I want to dial a number on a image click.. for this I have dynamically created div tags which contains such images. but I need to double click on image everytime to make a call.
I think it is happening because on first click wrap function is called and on second click tel is being invoked. I want to make call on first click.please suggest an alternative for this.
My code is:
for(var i=0;i<len;i++)
{
var maindiv=document.createElement("div");
maindiv.setAttribute("id", "maintablediv");
var table=document.createElement("table");
table.setAttribute("class","numbertable" );
table.setAttribute("border",0);
table.setAttribute("cellSpacing","0");
table.setAttribute("cellPadding","0");
var rowCount = table.rows.length ;
var row =table.insertRow(rowCount);
row.id="row";
var cell1 = row.insertCell(0);
if(i==0)
{
cell1.setAttribute("id","cmdcentralno");
}
var cell2=row.insertCell(1);
var p=document.createElement("p");
p.innerHTML=desc[i];
cell1.innerHTML=num[i];
cell1.setAttribute("class","numbername");
cell1.appendChild(document.createElement("br"));
cell1.appendChild(p);
cell2.setAttribute("class","phoneImg");
var image=document.createElement("img");
image.setAttribute("src", "images/Icon-Phone-Green.png");
if(i==0)
{
cell2.setAttribute("id","cmdcentralimage");
}
image.setAttribute("id", "imgphn"+i);
image.className="imgphn";
// var number=num[i].substring(num[i].indexOf('>')+1,num[i].indexOf('>')+14);
image.setAttribute("name", dialnum[i]);
cell2.appendChild(image);
image.setAttribute("align","centre");
maindiv.appendChild(table);
document.getElementById("numberMenu").appendChild(maindiv);
image.onclick=function()
{
var cellnumber=$(this).attr("name");
$(this).wrap("<a href =tel:"+cellnumber+"/>");
}
}
});
Untested, but I expect this should solve your problem...
// move this outside of the click handler (and use `image` instead of `this`)
image.wrap("<a href =tel:"+cellnumber+"/>");
image.onclick=function()
{
var cellnumber=$(this).attr("name");
}