I m new to scripting, and I started with Javascript. I m creating a script that takes value from a drop down list and process the input with the chosen value. Since HTML is not accepted in javascript files (.js) , I tried inserting the HTML select list as a script ,so that it may work. But that doesn't seem working. I have included my code below. Need some help in inserting a HTML code in Javascript. Thanks in advance.
My Code:
window.onload = function() {
var p = document.createElement("div")
p.innerHTML = 'Script to calculate the eligibility of a u20 player ' +
'<div style="padding-left:0em;"><b>U20 ELIGIBILITY CALCULATOR</b></div>'+ 'Years : ' +
'<div style=" display: inline-block; float: center; text-align: center;">' +
'<select id="x">'+
'<option value="17">17 yrs</option>' +
'<option value="18">18 yrs</option>' +
'<option value="19">19 yrs</option>' +
'<option value="20">20 yrs</option>' +
'</select>' +
'</div>' +
'<input type = "button" id = "calc" value = "Calculate Age" onclick = "calc_age()"/>' + 'He will be'+
'<input type = "text" id = "w" size="1" readonly />';
}
function calc_age() // to find age at the registration deadline
{
var n1,n2;
n1=document.getElementById("x").value;
n2=n1*2; // i have changed the operations here
document.getElementById("w").value=n2;
}
After creating the element
p
, you'll have to append it somewhere in the document. But first, instead of innerHTML, I would create the elements and append them to p, like so:Also, the way to get a select-box's value is to get the selected index, then find that value.