Jquery to capture and output information from html form - javascript

I'm having problems with this bit of code. For some reason it won't capture the radSize, and it's giving me problems. Any ideas?
The page is supposed to capture the values, then add the base and size together to output total.
$(function() {
$("#btnMessage").click(function() {
var name = $("input[name=txtName]").val();
var phone = $("input[name=txtPhone]").val();
var basePizza = $("#cboBase").val();
var size = $("input[name=radSize]").is(":checked").val;
// alert(name +" "+ phone +" "+ basePizza +" "+ size);
var message = "";
var calculation = parseInt(basePizza) + parseInt(size);
//test each value
if (name == "")
message += "--Enter a first name";
if (phone == "")
message += "\n--Provide a number";
if (basePizza == "0")
message += "\n--Pick Base Pizza";
if (size == "")
message += "\n--Pick a Size";
else
message += name + "," + " " + "Your total for the pizza will be " + "$" + calculation;
alert(message);
// $('#output').html(message);
});
});

This line isn't really valid:
var size = $("input[name=radSize]").is(":checked").val;
The .is(":checked") part is a jQuery filtering utility that will return true or false.
Instead, you want to do something like this:
var size = $("input[name='radName']:checked").val();
Assuming only one of the "radSize" checkboxes can be checked, $("input[name='radName']:checked") will return the checked checkbox and val() will return its value.

Related

Copy command in html for chrome to copy multiple text boxes

I am creating a note pad that is to help keep notes consistent between users. I am unable to copy the multiple text boxes to a string. I have attached all of my Java Script.
The copy button that I would like to use to link the multiple text boxes into one string of text. the reset button works at clearing the page and the copy button follows the not empty text box checks. Please help with my copy string to the clipboard.
I have tried a bunch of different sites on the java script with no success. I have also reviewed Stack Overflow to see if I could find a close project.
input type="button" id="BtnSupSubmit" value="Copy" onclick="notEmptySup()" style="width: 87px"
function settime() {
var curtime = new Date();
var curhour = curtime.getHours();
var curmin = curtime.getMinutes();
var time = "";
if (curhour == 0) curhour = 12;
time = (curhour > 12 ? curhour - 12 : curhour) + ":" +
(curmin < 10 ? "0" : "") + curmin + ":" +
(curhour > 12 ? "PM" : "AM");
document.date.clock.value = time;
clock = time
window.status = time
}
function notEmptySup() {
var myTextField = document.getElementById('TxtBoxCallersName');
if (myTextField.value != "") notEmptySup2()
else
alert("Please enter callers name.")
}
function notEmptySup2() {
var myTextField = document.getElementById('TxtBoxSupIssue');
if (myTextField.value != "") notEmptySup3()
else
alert("Please enter the reason for the escalation.")
}
function notEmptySup3() {
var myTextField = document.getElementById('TxtBoxSupAction');
if (myTextField.value != "") notEmptySup4()
else
alert("Please enter the action you took to help the customer.")
}
function notEmptySup4() {
var myTextField = document.getElementById('TxtBoxSupResolution');
if (myTextField.value != "") CreateMessage()
else
alert("Please enter the resolution of the call.")
}
function CreateMessage() {
strMessage =
"Time: " + clock + "\|" +
"***Supervisor Escalation" + "\***|" +
"Caller: " + document.getElementById("TxtBoxCallersName").value + " \| " +
"Reason: " + document.getElementById("TxtBoxSupIssue").value + " \| " +
"Action: " + document.getElementById("TxtBoxSupAction").value + " \| " +
"Resolution: " + document.getElementById("TxtBoxSupResolution").value + " \| " +
"Ticket Number: " + document.getElementById("TxtBoxSupTicketNumber").value + " \| " +
"Addl Notes: " + document.getElementById("TxtBoxSupNotes").value;
document.getElementById("hdnBuffer").value = strMessage;
var buffer = document.getElementById("hdnBuffer").createTextRange();
buffer.execCommand("Copy");
}
Most of what you have is redundant. See comments inline below:
// Get a reference to the form
let frm = document.querySelector("form")
// Set up a sumbit event handler for the form
frm.addEventListener("submit", function(evt){
// Just get the locally formatted time
var message = "Time: " + new Date().toLocaleTimeString() +
"\n***Supervisor Escalation***\n\n";
// Get all the input elements
let inputs = document.querySelectorAll("input");
// Loop over them
for(let i = 0; i < inputs.length; i++){
if(inputs[i].value === ""){
alert("Please enter the " + inputs[i].dataset.message);
inputs[i].focus(); // Put focus on bad element
evt.preventDefault(); // Cancel the form submit
break; // Exit the loop
} else {
// Update the message
message += inputs[i].dataset.message + ": " +
inputs[i].value + " \n";
}
}
alert(message); // Do whatever you want with the message
});
<form action="https://example.com" method="post">
<div><label>Name:<input data-message="callers name"></label></div>
<div><label>Issue: <input data-message="reason for the escalation"></label></div>
<div><label>Action: <input data-message="action you took to help the customer"></label></div>
<div><label>Resolution: <input data-message="resolution of the call"></label></div>
<button type="submit">Submit Ticket</button>
</form>

I'm trying to clear the input field after the button is clicked

I need the input field to clear after the user clicks the button to convert the number they've entered. I'm having a difficult time figuring this out, if anyone can help i feel like it's a very simple solution but, I can't seem to wrap my head around it.
(function () {
//Constants
const KM_TO_MILES = 0.625;
const MILES_TO_KM = 1.6;
var user = prompt("So..What's your name beautiful?");
if (user === null) {
alert("NOOOOO, you cancel me? meanie.")
remove();
}
//on load function
window.onload = function () {
var result = document.getElementById("result");
//display the user's name with a message prompt to continue to enter a distance
result.innerHTML = "Okay, " + user + ", enter your distance and I will calculate for you, don't worry.";
document.getElementById("convertBtn").onclick = startConvert;
};
//on load function done
//conversion function
function startConvert() {
var placeholder = document.getElementById("distance").value;
var distanceInput = document.getElementById("distance").value;
var conversion = document.getElementById('List').value;
document.getElementById("List").value;
// If the user doesn't input a number run the alert
if ((placeholder === "") || (conversion == "Select Types")) {
alert("You didn't enter anything mate");
// If the user inputs a number and clicks KM to M then calculate it and in the html page change the text to show the answer.
} else if (conversion == "Kilometers to Miles") {
document.getElementById("result").innerHTML = "Okay, " + user + " ,the distance of " + distanceInput + " is equal to " + (distanceInput * KM_TO_MILES + " miles.");
// If the user inputs a number and clicks M to KM then calculate it and in the html page change the text to show the answer.
} else if (conversion == "Miles to Kilometeres") {
document.getElementById("result").innerHTML = "Okay, " + user + " ,the distance of " + distanceInput + " is equal to " + (distanceInput * MILES_TO_KM + " kilometers.");
}
}
//conversion function done
}());
document.getElementById('yourid').value = '';
you call this event on your button click surely it'll be works

Why do I get a warning - JavaScript string return

function getCommonCategory() {
var grade = document.getElementById("grade").value;
var quizType = document.getElementById("quizType").value;
var moduleNumber = document.getElementById("moduleNumber").value;
var assignmentNumber = document.getElementById("assignmentNumber").value;
var category = "$CATEGORY: $course$/Default for " + grade;
if (moduleNumber == "")
alert("Enter the module number, please!");
var shortType = "";
if (quizType == "Homework")
shortType += "HW";
else
if (quizType == "Classwork")
shortType += "CW";
else
shortType += "QZ";
category += shortType + "/" + grade + " - Module " + moduleNumber + "/";
category += shortType + " - " + assignmentNumber + "/";
return category;
I'm doing a project in WebStorm and I have this separate JS-file in which I have this function. However, I keep getting a warning message saying that Returned expression type string is not assignable to type the. (it doesn't show the rest)
I also have this function:
function getListeningGift() {
var cat = getCommonCategory();
var win = window.open("");
win.writeln(cat);
}
and WebStorm says in it: Argument type is not assignable to type string
I can't understand what the problem is here. So what is it?

jquery check if number is typed isNaN

I have a quick form field where you can enter your age. When the user enters his/her age in words (like: twenty), it should give a quick alert telling that the age should be in numbers.
This is my code and it doesn't work
var formAge = $("<input class='inputClass' type='text' placeholder='bv.: 21'/>").appendTo(formDiv);
$(formAge).blur(function(){
if($(this).val() == isNaN){
alert("insert a number");
}
})
if(isNaN($(this).val())){
alert("insert a number");
}
You need to use javaScript isNaN() function.
Change your code like
if(isNaN($(this).val()))
{
alert("insert a number");
}
var a = isNaN(123) + "<br>";
var b = isNaN(-1.23) + "<br>";
var c = isNaN(5-2) + "<br>";
var d = isNaN(0) + "<br>";
var e = isNaN("Hello") + "<br>";
var f = isNaN("2005/12/12") + "<br>";
var res = a + b + c + d + e + f;
Output
false
false
false
false
true
true

Javascript Concatenate FirstName, LastName and Code

I am trying to concatenate the first letter of both names onto the randomly generated code.
var firstname = prompt("Please enter your first name.");
var lastname = prompt ("Please enter your last name.");
if (amountCorrect >= 4){
alert("Your login Code for the store is: " + str(firstname,1,1) + str(lastname,1,1) + (generateCode())); // Do the generateCode function
}
else{
alert("You have not passed on this occasion. You will now be taken back to the homepage.");
window.history.go(-1); // Go back a step
}
}
function generateCode(){
var text = "";
var possible = "0123456789";
for( var i=0; i < 4; i++ ){
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
There is no definition for 'str' used within your alert line..
alert("Your login Code for the store is: " + firstname[0] + lastname[0] + (generateCode()));
The above should work. It also looks like you have a stray } after the if block. You should probably also be doing more error checking for the user input, if the user doesn't enter a value at the prompt for example, you will get undefinedundefined#### where #### is the generated code.
You can use substring to extract the first character:
alert("Your login Code for the store is: " + firstname.substring(0,1) + lastname.substring(0,1) + (generateCode()));
Here is the documentation for String.prototype.substring.
Replace the line
alert("Your login Code for the store is: " + str(firstname,1,1) + str(lastname,1,1) + (generateCode())); // Do the generateCode function
with
alert("Your login Code for the store is: " + firstname.substring(1, 0) + lastname.substring(1, 0) + (generateCode())); // Do the generateCode function

Categories

Resources