I have this form in a html file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href='https://fonts.googleapis.com/css?
family=Playfair+Display:400,400italic,700,700italic' rel='stylesheet'
type='text/css'>
<TITLE>Test Input</TITLE>
</head>
<body>
<form name="compoundSearchForm" id="compoundSearchForm" action="#"
method="POST" onsubmit="return validateForm(this.form)">
<h1 id="header1"></h1>
<section>
<fieldset>
<div><label for="CAS">CAS NO</label>
<input type="RADIO" id="CAS" name="identifier" value="CAS" checked></div>
<div><label for="inchikey">InChiKeys</label>
<input type="RADIO" id="inchikey" name="identifier" value="inchikey">
</div>
<div><label for="smiles">SMILES</label>
<input type="RADIO" id="smiles" name="identifier" value="smiles"></div>
<div><label for="keyword">Keyword</label>
<input type="RADIO" id="keyword" name="identifier" value="name"></div>
<textarea id='userInput' type='textarea' name="userInput" Enter your
rows="15" cols="60" wrap="virtual">Enter your search here...</textarea>
<br/>
<button type="reset">RESET</button>
<button type="submit" name="compoundSearchButton"
id="compoundSearchButton" value="SEARCH" style="margin-right: 15px;"
>Search</button>
</fieldset>
</section>
</form>
<script type="text/javascript" src="bundle.js"></script>
<script type="text/javascript" src="main.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</body>
</html>
main.js contains my javascript code. bundle.js is for browserify. Here are the contents of the main.js file
//for radio buttons, get checked value
function identifierChecked(fieldName) {
console.log(fieldName);
var checkedValue;
for(var x=0;x<fieldName.length;x++) {
if(fieldName[x].checked) {
checkedValue=fieldName[x].value;
//console.log(checkedValue);
}
}
return checkedValue;
}
let selectedIdentifier;
document.getElementById('compoundSearchForm').onclick = function (){
selectedIdentifier=identifierChecked(document.getElementsByName('identifier'));
console.log(selectedIdentifier);
}
var inputValue=document.getElementById('userInput').value;
console.log(inputValue);
function validateForm (){ //enter code here }
validateForm();
I cannot seem to pass the exact values for the variables for the text area (inputValue) and radio buttons (selectedIdentifier) once the form is submitted. what is missing or what am I doing wrong?
please note, all my js files are being executed on a nodejs server. thank you.
The example below shows you how you can get the values of from your radio buttons and your textarea. It places the values in a div below the form to show the results:
function validateForm() { var identifier = document.getElementsByName("identifier"); for (var x = 0; x < identifier.length; x++) { if (identifier[x].checked) { formResults.innerHTML = identifier[x].value; } } formResults.innerHTML = formResults.innerHTML + " , " + userInput.value; return false; }
<form name="compoundSearchForm" id="compoundSearchForm" action="#" method="POST" onsubmit="return validateForm(this.form)"> <h1 id="header1"></h1> <section> <fieldset> <div><label for="CAS">CAS NO</label> <input type="RADIO" id="CAS" name="identifier" value="CAS" checked></div> <div><label for="inchikey">InChiKeys</label> <input type="RADIO" id="inchikey" name="identifier" value="inchikey"> </div> <div><label for="smiles">SMILES</label> <input type="RADIO" id="smiles" name="identifier" value="smiles"></div> <div><label for="keyword">Keyword</label> <input type="RADIO" id="keyword" name="identifier" value="name"></div> <textarea id="userInput" type="textarea" name="userInput" rows="15" cols="60" wrap="virtual">Enter your search here...</textarea> <br/> <button type="submit" name="compoundSearchButton" id="compoundSearchButton" value="SEARCH" style="margin-right: 15px;">Search</button> </fieldset> </section> </form> <div id="formResults" style="background-color: coral"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.