javascript, take value from form [duplicate] - javascript

This question already has an answer here:
javascript take value from array
(1 answer)
Closed 8 years ago.
<form name="car" onsubmit="return validate()">
<input type=text name=input[][] value="......" />
how can I alert value from filed input that is an field array?
function validate(){
alert("meee");
var mycar = document.car.elements['input[][]'].value; // this line does NOT want to store value in mycar
alert(mycar);
}
in this case alert box with message mee is displayed but another box that should display value mycar is not displayed.

simply assign an id or class to the input and use to access it.
As an example :
<input id="mycar" type="text" name=input[][] value="..." />
and the related JS
function validate() {
var mycar = document.getElementById('mycar');
var value = mycar.value;
}
if you insist on using the name, you can reference HTML form input tag name element array with JavaScript

Related

Trying to create an age restriction with if statement on javascript [duplicate]

This question already has answers here:
Clicking a button within a form causes page refresh
(11 answers)
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 2 years ago.
I'm trying to write an if statement code to redirect users below 18 to a separate website from users above and equal to 18 but the code isn't running, can someone figure out and explain what the problem is?
<body>
<div>
<form>
How old are you?
<br>
<br>
<input type="text" id="number">
<button onclick = submit();>Submit</button>
</form>
</div>
<script>
function submit() {
var number= document.getElementById("number");
if (number >= 18 ) {
location.href = "inec.com.ng"
;}
else if (number== isNaN){
alert('Input a number');
}
else {location.href = "www.google.com";}
}
</script>
You're trying yo use the DOM element itself instead of using its value
.
Once you get its value, don't forget to cast is to an int with parseInt:
var number = parseInt(document.getElementById("number").value);

Automize array object display from array database

I have an array containing 10 objects. Each contains a question string, a correctAnswer string, and an object with 4 answer strings:
const DB = [
{
question: "some question",
answers: ["a", "variety", "of", "choices"],
correctAnswer: "variety"
}, ...
I have a function which captures the user's answer via radio button input, and saves it in a variable:
function feedbackPage(){
$('.js-quizform-questions').on('click', '.js-button-next', function(event){
event.preventDefault()
let yourAnswer = getFeedback()
$('.js-feedback-page').show().html(evalCorrectAnswer(DB))
})
}
You see that evalCorrectAnswer reaches out to the above database for validation. At the moment i am only able to display and validate the hardcoded first answer, which is dynamically generated like this:
function generateQuestionElement(item) {
return `
<h2>${item[0].question}</h2>
<form id='form'>
<fieldset>
<div class='css-answers'>
<input id='answer1' type='radio' name='answer' required>
<label for='answer1'>${item[0].answers[0]}</label>
</div>
<div class='css-answers'>
<input id='answer2' type='radio' name='answer' required>
<label for='answer2'>${item[0].answers[1]}</label>
I need a way to automatically choose the first question, to include its answers and display it via generateQuestionItem. Then I need to take that question (& its answers) out of the pool and display the next question once i click on a Next button on the eval page. I have a hard time implementing this feature in an object-oriented way.
Codepen
Construct a variable of
let questionNumber = 0
Then update the following line inside renderQuestion:
<label for='answer1'>${DB[questionNumber].answers[0]}</label>
Later create a function that updates questionNumber at each appropriate point.

What is the difference between .getAttribute("name") and .name? [duplicate]

This question already has answers here:
getAttribute() versus Element object properties?
(7 answers)
Closed 5 years ago.
I have a simple web-application with an input text field in it looking like this:
<input id="txtip" type="text" value="10.1.1.50" />
The address 10.1.1.50 is a default value for an ip address. And from javascript I would read it like this:
txtip.getAttribute("value")
Now let's suppose to change it to 10.1.1.49. In google chrome the above javascript code will still return 10.1.1.50, while the expression
txtip.value
returns 10.1.1.49.
What is the difference? What is the "right way"?
var el = document.getElementById('testBox');
$(document).focusout(function () {
alert('el.value = ' + el.value);
alert('el.getAttribute("value") = ' + el.getAttribute('value'));
e.preventDefault();
});
<h2>Change value in the text box</h2>
<input id="testBox" type="text" value="original value" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
Found this on web might help you try following code type something and focusout
The difference is that element.value is real time and if a user changes let's say, a textbox input, it will reflect that, and show you the new value.
While getAttribute('value') will still show the original value="whateverWasHere" value.

Why can't I display a variable in a text field? [duplicate]

This question already has answers here:
Set the value of an input field
(17 answers)
Closed 6 years ago.
I have a function that creates a string and I would like for it to be displayed in a textbox on the page. I cannot understand why this does not work.
HTML:
<input id="poNum" type="text" name="PO" id="PO" style="width: 310px;" readonly />
JS:
document.getElementById("poNum").innerHTML = number;
document.getElementById("poNum").value = number;
InnerHTML is accessing... well, the actual inner html. Where as value is a attribute for inputs (selects, buttons, etc).
Use document.getElementById("poNum").value = number;
<input> elements have values, not inner HTML:
document.getElementById("poNum").value = number;

HTML Form Data into Ruby Formula

I'm trying to create a page where I can type into a textbox, on that page, and then create a variable using the data.
So far, I have:
<FORM NAME = frmName>
First Nme: <INPUT TYPE = Text NAME = txtFirst value ="">
Last Nme: <INPUT TYPE = Text NAME = txtLast value ="">
<P>
Total: <INPUT TYPE = Text NAME = txtFullName value = "">
<P>
<Input Type = Button NAME = b1 VALUE = "Submit" onClick = calculate()>
</FORM>
The JavaScript is working that allows me to run the onClick command, but what I really need has to have a page where I can insert data, and then populate a variable with that data.
For example. Type my first name and last name into the textboxes on the page. Then a variable running in the background, with be created. (ex. variable = first + name). This variable can then be used in the code on the rest of the page. Please also keep in mind, I don't want to add the form data into a database.
Is it possible to do this in ruby only?

Categories

Resources