Ho can I can I create a textbox with javascript and then onclick, pass a function with argument? below the commented out line is the problem
var inputBox = document.createElement("input");
inputBox.staff = project.staff[j].name;
inputBox.defaultValue = inputBox.staff;
inputBox.onchange = changeHours;
//inputBox.onclick = selectText(this);
inputBox.onclick=function(argument){
selectText(this);
};
Try
inputBox.onclick=function(){selectText(this)};
http://www.w3schools.com/jsref/event_onclick.asp
Just set onclick with the function that you want.
var inputBox = document.createElement("input");
inputBox.staff = project.staff[j].name;
inputBox.defaultValue = inputBox.staff;
inputBox.onchange = changeHours;
inputBox.onclick = function() {
selectText(this);
};
Related
I am trying to open a new window with a custom form in javascript
In the form I am having some fields and action along with the submit input (please note that the element should be of input type=submit
However, when clicking the submit button in the popup window, the submit action is not performed.
var map = window.open("","editormap",'width=600,height=600,scrollbars=no,resizable=no');
var mapForm = map.document.createElement('form');
mapForm.target = "editormap";
mapForm.id="inpForm";
mapForm.method = "POST";
mapForm.action = "/dashboard/save/"+val1+"/"+val2;
var mapInput3 = map.document.createElement('input');
mapInput3.type = 'submit';
mapInput3.name = 'submit';
mapInput3.value = 'Submit';
mapForm.appendChild(mapInput3);
var mapInput = map.document.createElement('input');
mapInput.type = "button";
mapInput.name = "cancel";
mapInput.value = "Cancel";
mapInput.onclick=map.close;
mapForm.appendChild(mapInput);
var mapInput1 = map.document.createElement("textarea");
mapInput1.name = "code";
mapInput1.value = repoValues;
mapForm.appendChild(mapInput1);
var mapInput2 = map.document.createElement("input");
mapInput2.type = "hidden";
mapInput2.name = "filename";
mapInput2.value = 'some data';
mapForm.appendChild(mapInput2);
map.document.body.appendChild(mapForm);
I have also tried bind like below
var parentForm = document.createElement.bind(document);
var mapForm = parentForm('form');
still the submit is not performing the requested action.
Please help or guide in the right direction.
Since the window has an empty location, relative URLs have no base URL to be interpreted relative to. So you need to provide the full URL in the action URL.
mapForm.action = "https://www.example.com/dashboard/save/"+val1+"/"+val2;
DEMO
(Stack Snippets don't allow popup windows, so I had to use jsfiddle for the demo.)
Hello I am trying to make my jquery code in working order but its not working at all, I don't know whats a problem behind it but it contains multiple text boxes in multiple rows, each row calculates its own sum
Here is Fiddle link
Here is my Code
$(document).ready(function(){
$('.employee input[type="text"]').keyup(function() {
var basic_salary = parseInt($('input[name^=txtMonthlyRate]').val());
var advance_salary = parseInt($('input[name^=txtAdvance]').val());
var recover_comm = parseInt($('input[name^=txtRecovery]').val());
var sales_comm = parseInt($('input[name^=txtSales]').val());
var deduction_salary = parseInt($('input[name^=txtDeduction]').val());
var adjustment_salary = parseInt($('input[name^=txtAdjustment]').val());
var total_sum = ((basic_salary+recover_comm+sales_comm) - (deduction_salary + advance_salary)) + adjustment_salary;
$('input[name^=txtTotal]').val(total_sum);
console.log(total_sum)
);
});
The txtSales1, txtDeduction1, txtAdjustment1 variables are camel cased in your javascript, but not on the html input name. So these return NaN.
UPDATE Also, you need to set the context of what you're referring to using the second parameter of a selector function:
$('.employee input[type="text"]').keyup(function(e) {
var $scope = $(this).closest('.employee');
var basic_salary = parseInt($('input[name^=txtMonthlyRate]', $scope).val());
var advance_salary = parseInt($('input[name^=txtAdvance]', $scope).val());
var recover_comm = parseInt($('input[name^=txtRecovery]', $scope).val());
var sales_comm = parseInt($('input[name^=txtSales]', $scope).val());
var deduction_salary = parseInt($('input[name^=txtDeduction]', $scope).val());
var adjustment_salary = parseInt($('input[name^=txtAdjustment]', $scope).val());
var total_sum = ((basic_salary+recover_comm+sales_comm) - (deduction_salary + advance_salary)) + adjustment_salary;
$('input[name^=txtTotal]', $scope).val(total_sum);
});
The txttotal1 needs to be changed to txtTotal1
The fiddle needs a closing }
I use JavaScript to generate form input fields on my page. Everything works fine, except the button. I came across some problems generation the button's onClick function using DOM. You can see the code below, but on my page, there is no the onClick function as the attribute of the input button tag:
n = 1;
function generate() {
var radiomore = document.createElement("input");
radiomore.type = "button";
radiomore.name = "opt[" + n + "]";
radiomore.value = "Add more options";
radiomore.setAttribute = ('onClick',addradiomore);
var div = document.createElement("div");
div.innerHTML = "Op: " + radiomore.outerHTML + "<br/>";
document.getElementById("mydiv").appendChild(div);
n++;
}
function addradiomore() {
//here goes the function code
}
And this is what it generates on my page:
<input type="button" name="opt[1]" value="Add more options">
There is no function?!?!
P.S.
Even if I use like this it doesn't work:
radiomore.onclick = addradiomore();
You should use this:
radiomore.onclick = addradiomore;
DEMO
What about:
radiomore.onclick = function () {
addradiomore();
};
I am trying to create a table dynamically as soon as a page loads. In the code below, I get a response when I click the button, but the table is not displayed on the page. What's wrong with the code below? I have looked at other discussion threads on this topic, but none have helped.
The code in the Javascript file is as follows:
function showalert() {
alert('what?');
}
function displayGuides() {
var mgdCell, mgdRow, mgdTable;
mgdTable = document.createElement('table');
mgdRow = mgdTable.insertRow(0);
mgdCell = mgdRow.insertCell(0);
mgdCell.innerHTML = "1111";
mgdCell = mgdRow.insertCell(1);
mgdCell.innerHTML = "2222";
mgdCell = mgdRow.insertCell(2);
mgdCell.innerHTML = "3333";
mgdCell = mgdRow.insertCell(3);
mgdCell.innerHTML = "4444";
mgdCell = mgdRow.insertCell(4);
mgdCell.innerHTML = "5555";
document.getElementByID('mgdTable').appendChild(mgdTable);
}
function mgdUserActions() {
var create = document.getElementById('create');
create.onclick = showalert;
displayGuides();
}
window.onload = mgdUserActions;
Your call in displayGuides to document.getElementByID should be document.getElementById. The 'D' in ID should be 'd' Id.
Check out the fiddle of awesomeness
I'm trying to add a checkbox to my form which copies billing details from the form to shipping details, but my function isn't working.
This is what I have so far:
function fillShipping(f) {
var billing = document.forms["billing"];
var shipping = document.forms["shipping"];
if (document.getElementById("checkshipping").checked)
{
f.title.value = f.title.value;
f.firstname.value = f.firstname.value;
f.email.value = f.email.value
f.phone.value = f.phone.value
f.Address1.value = f.Address1.value
f.Address2.value = f.Address2.value
f.city.value = f.city.value
f.postcode.value = f.postcode.value
f.country.value = f.country.value
}
Can anyone point out where I'm going wrong?
You probably meant
shipping.title.value = billing.title.value;
Instead of
f.title.value = f.title.value;
if you are getting the forms and putting them into vars why are you passing in f? why not billing.email.value = shipping.email.value? Also how are you calling this function?