I'm creating a form and it asks candidates to post info about their previous education history - here are the screenshots of the form and the code attached to them!
Here's my javascript code:
<script>
function removeFields1(){
//var container1 = document.getElementById("container1");
//container1.removeChild(input);
}
function addFields1(){
var container = document.getElementById("container1");
var option = document.createElement("select"); //? how do I fix this up
//option.text = "Kiwi";
//container.add(option);
container.appendChild(option);//? how do I fix this up
container.appendChild(document.createTextNode("Address: "));//Address form
var input = document.createElement("input");
input.type = "text";
input.id = "instaddress";
input.name = "instaddress";
input.size = 20;
input.maxlenth = 20;
container.appendChild(input);
container.appendChild(document.createTextNode("From: ")); // which year the person started
var from = document.createElement("input"); // studying in that institution
from.type = "text";
from.id = "from";
from.name = "from";
from.size = 4;
from.maxlenth = 4;
container.appendChild(from);
container.appendChild(document.createTextNode("To: ")); // which year the person finished
var to = document.createElement("input"); // studying in that institution
to.type = "text";
to.id = "to";
to.name = "to";
to.size = 4;
to.maxlenth = 4;
container.appendChild(to);
container.appendChild(document.createTextNode(" Did You Graduate?: Yes")); // radio buttons whether someone graduated or not
var grad = document.createElement("input");
grad.type = "radio";
grad.id = "graduate";
grad.name = "graduate";
grad.value = "yes"; //yes value for radio button
container.appendChild(grad);
container.appendChild(document.createTextNode(" No "));
var grad1 = document.createElement("input");
grad1.type = "radio";
grad1.id = "graduate";
grad.value = "no"; //no value for radio button
container.appendChild(grad1);
container.appendChild(document.createTextNode(" Certificate: "));
var certificate = document.createElement("input");
certificate.type = "text";
certificate.id = "certificate";
certificate.name = "certificate";
input.size = 25;
input.maxlenth = 25;
container.appendChild(certificate);
var addInstitution = document.getElementById(" Add");
var removeInstitution = document.getElementById("Remove");
// container.removeChild(addInstitution);
//create and insert input/text
//create and insert button
addInstitution = document.createElement("a");
addInstitution.id="Add"
addInstitution.href="#";
addInstitution.text="Add";
addInstitution.onclick=function(){addFields1();};
removeInstitution = document.createElement("a");
removeInstitution.id="Remove"
removeInstitution.href="#";
removeInstitution.text=" Remove";
container.appendChild(addInstitution);
container.appendChild(removeInstitution);
//removeInstitution.onclick=function(){removeFields1();};
//
container.appendChild(document.createElement("br"));
}
</script>
Here are the form fields as well:
<body>
<form>
<div id="container1">
<select name="institution" id="institution">
<option <?php if(isset($_POST['institution'])) { echo $_POST['institution']; } ?>>Select Institution</option>
<?php
$sql1a = "SELECT * FROM institution ORDER BY institution asc";
$smt1a = $dbs->prepare($sql1a);
$smt1a -> execute();
while($row1a=$smt1a->fetch(PDO::FETCH_ASSOC))
{
if($row1a['institution']==$_GET['id3'])
echo("<option selected value=$row1a[institution]>$row1a[institution]</option>");
else
echo("<option value=$row1a[institution]>$row1a[institution]</option>");
}
?>
</select>
Address: <input size="20" type="text" id="instaddress" name="instaddress" maxlength="20" size="20"> From:<input type="text" id="from" name="from" size="4" > To: <input type="text" id="to" name="to" size="4">
Did You Graduate?: Yes<input type="radio" onclick="checkRadio()" id="graduate" name="graduate" value="yes"> No
<input type="radio" onclick="checkRadio()" id="graduate" name="graduate" value="no"> Certificate: <input size="20" type="text" id="certificate" name="certificate" maxlength="25" size="25">
Add <br>
</div>
</form>
</body>
How can I create a drop-down select menu for the Javascript section when I click addFields1()?
The PHP code for the drop down menu is down here below-the menu is populated with data from a MySQL database. What will be the correct
<option <?php if(isset($_POST['institution'])) { echo $_POST['institution']; } ?>>Select Institution</option>
<?php
$sql1a = "SELECT * FROM institution ORDER BY institution asc";
$smt1a = $dbs->prepare($sql1a);
$smt1a -> execute();
while($row1a=$smt1a->fetch(PDO::FETCH_ASSOC))
{
if($row1a['institution']==$_GET['id3'])
echo("<option selected value=$row1a[institution]>$row1a[institution]</option>");
else
echo("<option value=$row1a[institution]>$row1a[institution]</option>");
}
?>
</select>
Can any of you guys help me out with fixing up the Javascript code?
Here's the snippet of code that needs to be rectified so that I can use Javascript functions to drop down the menu list every time that the "Add" link is pressed:
var option = document.createElement("select"); //? how do I fix this up
var option = document.createElement("select"); //? how do I fix this up
//option.text = "Kiwi";
//container.add(option);
container.appendChild(option);//? how do I fix this up
i think what you want to do is:
var select = document.createElement("select");
var option = document.createElement("option");
option.value = "Kiwi";
option.innerHTML = "Kiwi";
select.appendChild(option);
....but producing these long javascript functions is not a good programming style...a function should consist of no more than 20 to 30 lines of code...so you should consider using one of these:
http://www.sitepoint.com/10-javascript-jquery-templates-engines/
or load the whole line via ajax...
Related
I want to create two dimentional array matrix 2 inside javascript for input. But all of record datas are inside first input. I have to show relevant record data every input. Can you help me for two dimentional array.
HTML CODES
<div id="hiddenProduct">
<label>Company Product Number</label>
<select onchange="addProducts()" name="firmProductNumber" id="prdcts" class="form-control form-control-sm" style="width: 100%;">
<option value="0">Unselected</option>
<option #if($saproduct->firmProductNumber == 1 ) selected="true" id="btnProduct" onclick="addProducts()" #endif>1</option>
<option #if($saproduct->firmProductNumber == 2 ) selected="true" id="btnProduct" onclick="addProducts()" #endif>2</option>
</select>
<hr>
</div>
</div>
<div id="product" class="form-group col-md-4">
<div id="hiddenContainerP">
<div id="containerP"></div>
</div>
</div>
JavaScript Codes
function addProducts() {
var number = document.getElementById("prdcts").value;
var container = document.getElementById("containerP");
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}
var p = [];
p = <?php echo json_encode($x); ?>
//Controller $x = []; $x[] = [$arrayProductName,$arrayProductAmount,$arrayProductAmountType];
for (i = 0; i < number; i++) {
// Append a node with a random text
// Create an <input> element, set its type and name attributes
var productName = document.createElement("input");
productName.type = "text";
productName.name = "name[]";
productName.value = p[[0][i]];
productName.className = "form-control form-control-sm";
productName.placeholder = "Product Name";
container.appendChild(productName);
var amount = document.createElement("input");
amount.type = "number";
amount.name = "amount[]";
amount.className = "form-control form-control-sm";
amount.placeholder = "Amount";
amount.value = p[[1][i]];
container.appendChild(amount);
var amountType = document.createElement("input");
amountType.type = "text";
amountType.name = "amountType[]";
amountType.value = p[[2][i]];
amountType.className = "form-control form-control-sm";
amountType.placeholder = "Amount Type";
container.appendChild(amountType);
container.appendChild(document.createElement("br"));
}
}
</script>```
I want to assign a drop down option to a unique ID so that when it is selected, the javascript will take that ID and turn it into a variable which I can use to display the cost.
This is what I have so far:
<form>
<select>
<option name ="Bristol "value="40.0" id="BN1">Bristol - Newcastle</option>
</select>
<button type="button" onclick="BookingFare(); return false;">Submit</button><br>
</form>
function BookingFare() {
var price = 0;
//var ofAdults = document.getElementById('adults').value;
//var ofChildren = document.getElementById('children').value;
var BN1 = document.getElementById('BN1').value;
if (BN1) {
var price = price + 40;
}
document.getElementById('priceBox').innerHTML = price;
}
I can't get it to display the price, even when I just try to get it to print out "hello" when BN1 is selected it doesn't work. Can anyone tell me what I'm missing?
function BookingFare() {
var price = 0;
//var ofAdults = getElementById('adults').value;
//var ofChildren = getElementById('children').value;
var BN1 = document.getElementById('BN1').value;
if (BN1) {
var price = price + 40;
}
document.getElementById('priceBox').innerHTML = price;
}
<form>
<select id="BN1">
<option name ="Bristol "value="40.0" >Bristol - Newcastle</option>
</select>
<button type="button" onclick="BookingFare(); return false;">Submit</button><br>
</form>
<label id='priceBox'></label>
I have an issue concerning an 'onchange' event within a select html element. The code i have written is supposed to display certain text boxes depending on which select option has been selected.
The code I have is as follows:
Customer Type:
<select name="customerType" onchange="cust_type()">
<option value="" selected>Customer Type?</option>
<option value="nonCorp">Customer</option>
<option value="corp">Corporate</option>
</select>
JS:
function cust_type() {
var select_drop = document.getElementsByName('customerType');
var selected = select_drop[0].value;
var f_name = document.getElementById('forename');
var s_name = document.getElementById('surname');
var c_name = document.getElementById('companyName');
if(selected == "") {
f_name.style.visibility = 'hidden';
s_name.style.visibility = 'hidden';
c_name.style.visibility = 'hidden';
f_name.value = "";
s_name.value = "";
c_name.value = "";
}
if(selected == "nonCorp") {
f_name.style.visibility = 'visible';
s_name.style.visibility = 'visible';
c_name.style.visibility = 'hidden';
c_name.value = "";
}
if(selected == "corp") {
f_name.style.visibility = 'hidden';
s_name.style.visibility = 'hidden';
c_name.style.visibility = 'visible';
f_name.value = "";
s_name.value = "";
}
}
The problem I am experiencing is that when I change the option in the select menu the first time, the onchange has no effect. However on the second, third etc etc it seems to be working perfectly fine.
Thank you for taking the time to read my question.
Did you try to add the event in JavaScript ?
var yourSelectElement = document.getElementById('test');
yourSelectElement.onchange = cust_type;
Let see : http://jsfiddle.net/YtuxP/
Do you try to do that ?
Fix:
Instead of directly changing the visibility of the text boxes I wrapped them in separate divs. The code solution is as follows.
HTML:
Customer Type:
<select name="customerType" onchange="cust_type()">
<option value="" selected>Customer Type?</option>
<option value="nonCorp">Customer</option>
<option value="corp">Corporate</option>
</select>
<div id="nonCorpCustDetails" class="custDetails" style="visibility:hidden">
Forename <input type="text" name="forename" id="forename" onchange="submit_check()" />
Surname <input type="text" name="surname" id="surname" onchange="submit_check()" /.
</div>
<div id="corporateCustDetails" class="custDetails" style="visibility:hidden">
Company Name <input type="text" name="companyName" id="companyName" onchange="submit_check()" />
</div>
JS function:
function cust_type() {
var select_drop = document.getElementsByName('customerType');
var selected = select_drop[0].value;
var f_name = document.getElementById('forename');
var s_name = document.getElementById('surname');
var c_name = document.getElementById('companyName');
var nonCorp = document.getElementById('nonCorpCustDetails');
var corp = document.getElementById('corporateCustDetails');
if(selected == "") {
nonCorp.style.visibility = 'hidden';
corp.style.visibility = 'hidden';
f_name.value = "";
s_name.value = "";
c_name.value = "";
}
if(selected == "nonCorp") {
nonCorp.style.visibility = 'visible';
corp.style.visibility = 'hidden';
c_name.value = "";
}
if(selected == "corp") {
nonCorp.style.visibility = 'hidden';
corp.style.visibility = 'visible';
f_name.value = "";
s_name.value = "";
}
}
I'm developing an android app with phonegap. I'm trying to use an HTML form in one of the pages and I've been told that I should use HTML DOM. I've made the script but I got a error saying: Unexpected token.
Here's the sample code in HTML:
<form name="editNoteForm" id="formaltera" method="post" action="index.html">
<div data-role="fieldcontain">
<label for="primeiroNome">Primeiro Nome</label>
<input type="text" name="primeiroNome" id="primeiroNome">
</div>
<div data-role="fieldcontain">
<label for="ultimoNome">Ultimo Nome</label>
<input type="text" name="ultimoNome" id="ultimoNome">
</div>
<div data-role="fieldcontain">
<label for="numeroTelefone">Telefone</label>
<input type="number" name="numeroTelefone" id="numeroTelefone">
</div>
<div data-role="fieldcontain">
<input type="submit" id="submit" value="Guardar">
</div>
</form>
And here's the script I made so I can get something like the HTML code above.
<form name="formaltera" id="formaltera" method="post" action="index.html"></form>
<script charset="utf-8" language="javascript">
var temp1 = localStorage.getItem("selected");
var temp = localStorage.getItem(temp1);
temp = temp.split(";");
var firstName = temp[0];
var lastName = temp[1];
var phoneNumber = temp[2];
var form = document.getElementById('formaltera');
var formBody = form.childNodes[0];
var div = document.createElement('div');
var inputTextA = document.createElement('input');
var inputTextB = document.createElement('input');
var inputTextC = document.createElement('input');
var inputSubmit = document.createElement('input');
var labelA = document.createElement('label');
var labelB = document.createElement('label');
var labelC = document.createElement('label');
var labelTextA = document.createTextNode('Primeiro Nome');
var labelTextB = document.createTextNode('Ultimo Nome');
var labelTextC = document.createTextNode('Telefone');
div.data-role = "fieldcontain";
labelA.for = "primeiroNome"; //I'm getting an error in here
labelB.for = "ultimoNome";
labelC.for = "numeroTelefone";
inputTextA.type = "text";
inputTextA.name = "primeiroNome";
inputTextA.id = "primeiroNome";
inputTextA.value = firstName;
inputTextB.type = "text";
inputTextB.name = "ultimoNome";
inputTextB.id = "ultimoNome";
inputTextB.value = lastName;
inputTextC.type = "number";
inputTextC.name = "numeroTelefone";
inputTextC.id = "numeroTelefone";
inputTextC.value = phoneNumber;
inputSubmit.type = "submit";
inputSubmit.name = "submit";
inputSubmit.id = "submit";
inputSubmit.value = "Guardar";
form.appendChild(div);
div.appendChild(labelA);
labelA.appendChild(labelTextA);
div.appendChild(inputTextA);
form.appendChild(div);
div.appendChild(labelB);
labelA.appendChild(labelTextB);
div.appendChild(inputTextB);
form.appendChild(div);
div.appendChild(labelC);
labelA.appendChild(labelTextC);
div.appendChild(inputTextC);
form.appendChild(div);
div.appendChild(inputSubmit));
</script>
The HTML code could be a bit different from the script I've made because I wrote it just to elucidate you. Thanks in advance. Hope I'm explaining myself well.
the for attribute should be assigned through htmlFor property
labelA.htmlFor = ...
See https://developer.mozilla.org/en-US/docs/DOM/HTMLLabelElement for further reference
i am trying to create a bit of javascript that will create a new text field every time a button is pressed, any help would be appreciated, it seems like the javascript doesn't want to run more than once
<!DOCTYPE html>
<html>
<head>
<title>Q&A Admin Panel</title>
</head>
<body>
<h1>Q&A Admin Panel</h1>
<form action="add_question.php" method="post">
Question Type: <input type="text" id="questionType" />
Question Name: <input type="text" id="questionName" />
Question Text: <input type="text" id="questionText" />
<select id="myList" onchange="selectType()">
<option>Yes or No</option>
<option>Multiple Choice</option>
<option>Multiple Select</option>
<option>Open Response</option>
</select>
<div id='buttons'> </div>
</form>
<script type="text/javascript">
function selectType()
{
var type=document.getElementById("myList");
if(type == "Multiple Choice" or type == "Multiple Select"){
// add answer = visible
}
}
</script>
<script type="text/javascript">
var answers = 0;
function addAnswer()
{
write = document.getElementById('buttons');
write.innerHTML = write.innerHMTL + "add answer: <input type=\"text\" id=\"answer" + answers + "\" <br>";
answers = answers + 1;
}
</script>
<button onclick="addAnswer(); return false;">add answer</button>
</body>
</html>
var answers = 0,
write = document.getElementById('buttons');
function addAnswer() {
write.innerHTML += 'Add answer: <input type="text" id="answer"' + answers + '/> <br />';
answers++;
}
I faced the same problem in my college project. You can also accomplish your work as David suggested, but using innerHTML doesn't add elements to DOM and as a result, when you'll refresh the page, text fields will disappear. So for getting persistent text fields, you can use the code mentioned below:
var i = 0;
function addMore()
{
var x = document.getElementById('buttons');
var input1 = document.createElement("input");
input1.setAttribute("type","text");
input1.setAttribute("name","i" + i );
x.appendChild( input1 );
i++;
}
You can use firebug for debugging javascript things.
Thanks.
function addTextField(id){
var colors = new Array('#660000','#33ff00','#0066ff','#cc3399','#9966ff');
var container = document.getElementById(id);
var ulElement = document.createElement('ul');
container.appendChild(ulElement);
var hideLink = function(){
var firstElement = ulElement.firstChild.getElementsByTagName('a')[0];
firstElement.style.display = (ulElement.childNodes.length==1)?'none':'inline';
for(var i = 0 ; i <ulElement.childNodes.length; i++)
ulElement.childNodes[i].style.color = colors[i%5];
}
var addListElement = function(){
var liElement = document.createElement('li');
ulElement.appendChild(liElement);
var textElement = document.createElement('input');
textElement.setAttribute('type','text');
liElement.appendChild(textElement);
var deleteLink = document.createElement('a');
deleteLink.href = "#";
deleteLink.appendChild(document.createTextNode('delete'));
liElement.appendChild(deleteLink);
deleteLink.onclick = function(){
ulElement.removeChild(liElement);
hideLink();
}
hideLink();
}
addListElement();
var anchorElement = document.createElement('a');
anchorElement.href = "#";
anchorElement.appendChild(document.createTextNode('Add more'));
container.appendChild(anchorElement);
anchorElement.onclick = addListElement;
hideLink();
}
Here is the Demo
http://jsfiddle.net/mannejkumar/cjpS2/