I have setup a codepen so you can see what I want. Click here to open the codepen: https://codepen.io/tbellmer/pen/RdxBdQ
I am relatively new at this and while the form is functional, I need help with the aesthetics. I would like to have both the Action: and Comment: labels be right aligned. I am trying to use the most modern techniques so not looking to use a table or float.
My code uses a flexbox to have an aside section and main section. Can I use a flexbox or grid to do what I want? Also notice that I used a for some vertical spacing and suspect there is a better way to do this as well.
Your assistance will be greatly appreciated!
<div class="container">
<div class="flex-grid">
<aside class="col sidebar">
<p class = 'ctr'>Welcome</p>
</aside>
<section class="col main">
<h1 class='ctr'>Title</h1>
<hr>
<form action = '#'>
<label for = 'combo'>Action: </label>
<select id = 'combo' name = 'response' required>
<option value = ''>--none--</option>
<option value = 'A'>Approved</option>
<option value = 'R'>Rejected</option>
</select>
<p></p> <!-- has to be a better way to get space -->
<!-- want to have both Action: and Comment: labels line up to right -->
<label for = 'comment'>Comment: <label>
<input type=text id='comment' size='40' name='comment'>
<p></p>
<input class = 'button' type = 'submit' value = 'Submit' id = 'submit'>
<input class = 'button' type = 'reset' value = 'Cancel'>
</form>
</section>
</div>
</div>
You can create a div inside the form tag just like i did below and add a class with the
CSS
.display-align {
display: grid;
grid-template-columns: max-content max-content;
grid-gap:5px;
}
HTML
<form>
<div class="display-align">
<div>
<label for = 'combo'>Action: </label>
<br><br>
<label for = 'comment'>Comment: <label>
</div>
<div>
<select id = 'combo' name = 'response' required">
<option value = ''>--none--</option>
<option value = 'A'>Approved</option>
<option value = 'R'>Rejected</option>
</select>
<br><br>
<input type=text id='comment' size='40' name='comment'>
</div>
</div>
<br>
<div>
<input class = 'button' type = 'submit' value = 'Submit' id = 'submit'>
<input class = 'button' type = 'reset' value = 'Cancel'>
</div>
</form>
You can use the <br> tag to break a line or still use the <p> .
But may I suggest to you use this instead(its basically the same, but different lol).
I use the same CSS class in this code.
<form>
<fieldset style="max-width: 400px;">
<legend>Title</legend>
<div class="display-align">
<div>
<label for = 'combo'>Action: </label>
<br><br>
<label for = 'comment'>Comment: <label>
</div>
<div>
<select id = 'combo' name = 'response' required">
<option value = ''>--none--</option>
<option value = 'A'>Approved</option>
<option value = 'R'>Rejected</option>
</select>
<br><br>
<input type=text id='comment' size='40' name='comment'>
</div>
</div>
<br>
<div>
<input class = 'button' type = 'submit' value = 'Submit' id = 'submit'>
<input class = 'button' type = 'reset' value = 'Cancel'>
</div>
</fieldset>
</form>
Related
not able to clear the data using angular Js. I have assigned the value to null and empty. when i debug it the values are getting cleared but in front end the values are not clearing
$scope.callCancel = function() {
$scope.riskObj.areaName ="";
$scope.areaName ="";
$scope.riskObj.areaDesc ="";
$scope.areaDesc =""
html code:
<div ng-if="type == 1"class="col-md-3">
<h5><strong>Risk Area Name</strong></h5>
<div class="form-group">
<input type="text" class="form-control input-sm text-field" data-ng-model="areaName" ng-keyup="getChangedAreaName(areaName)" ng-disabled="type == 2">
</div>
</div>
<div ng-if="type == 1" class="col-md-6">
<h5><strong>Risk Area Description</strong></h5>
<div class="form-group">
<input type="text" class="form-control input-sm text-field" data-ng-model="areaDesc" ng-keyup="getChangedAreaDesc(areaDesc)" ng-disabled="type == 2">
</div>
</div>
<div ng-if="type == 1" style="text-align:right;">
<img src="core/static/images/submit-btn.png" data-ng-click="updateRiskArea(areaName,areaDesc,this)" />
<img src="core/static/images/cancel-btn.png" data-ng-click="callCancel()"/>
</div>
I need to clear the values I enter in the front end. Using the above code I am able to clear already entered data. But once I modify the value and call cancel function the values are not getting cleared
The main issue is you're not setting your ng-model accurately. For example, it should bind to riskObj.areaName as that is your data model, not just areaName. Additionally, you should be initializing these objects in your scope somewhere, like $scope.riskObj = {areaName:'', areaDesc:''};
There are a few issues with your code other than that.
Utilize your anchor tags to be ng-click. Don't have an anchor with a hash for HREF and then put an ng-click inside on an image. Instead, just do something like <a href ng-click="...
You have 3 divs in a row with ng-if="type==1" - why not just put those 3 in a <div class="row" ng-if="type==1"> - cleaner and easier to read/maintain
Inside those conditionals, you have a ng-disabled="type==2" -- that will never ever do anything since the div will ONLY show if type=1
angular.module('selectExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.type = 1;
$scope.riskObj = {
areaName: '',
areaDesc: ''
};
$scope.callCancel = function() {
$scope.riskObj.areaName = "";
$scope.areaName = "";
$scope.riskObj.areaDesc = "";
$scope.areaDesc = ""
}
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.10/angular.min.js"></script>
<div ng-app="selectExample">
<div ng-controller="ExampleController">
<div class='row' ng-if="type == 1">
<div class="col-md-3">
<h5><strong>Risk Area Name</strong></h5>
<div class="form-group">
<input type="text" class="form-control input-sm text-field" data-ng-model="riskObj.areaName">
</div>
</div>
<div class="col-md-6">
<h5><strong>Risk Area Description</strong></h5>
<div class="form-group">
<input type="text" class="form-control input-sm text-field" data-ng-model="riskObj.areaDesc">
</div>
</div>
<div style="text-align:right;">
<a href data-ng-click="callCancel()">cancel</a>
</div>
</div>
</div>
I have A form with several Items, including some check boxes that need to receive their status from the data service/ a model.
<div class="col">
<label for="expired">IsExpired</label>
<div>
<input type="checkbox" id="expired" disabled>
</div>
</div>
this is in the class of card.model.ts
expired: boolean;
and data.service.ts:
new CardModel('4',new Duration(2, 0), new LessonModel('#5DC878', 'Bialogy'), 'just the first part', false,true,new Date(2018,11,11,13,30),new Date(2018,11,11),true,false)
I have tried to put [(ngModel)] = "expired" into the input tag but I receive an error:
Can't bind to 'ngModel' since it isn't a known property of
'input'
what is the problem?
Try this:-
html
<div class ="col">
<label for="expired">IsExpired</label>
<div>
<input type="checkbox" id="expired" [checked]="checkedStatus">
<input type="checkbox" [(ngModel)]="checkedStatus" name="ele_name"/> <=== add name
</div>
</div>
ts
checkedStatus = true;
I am attempting to create an expandable form to create on/off instructions that a user can submit times for in pairings, so my HTML defaults with one pair and the user can use a button to add additional pairs, but when i submit the form angular is only reading the first pairing, can someone point out what I am missing here? Am I appending in the extra fields improperly?
HTML
<div class="timing">
<form class="timingSelect" action="index.html" method="post">
<div class="inputField">
<div class="form-group">
On: <input type="number" ng-model="recipe.on1" value="" step=".1">
</div>
<div class="form-group">
oz: <input type="number" ng-model="recipe.oz1" value="" readonly="readonly">
</div>
<div class="form-group">
Off: <input type="number" ng-model="recipe.off1" value="" step='.1'>
</div>
</div>
<!-- <input type="submit" ng-click="createRecipe(recipe)" value="Generate Recipe"> -->
</form>
<button type="submit" ng-click="createRecipe(recipe)">Submit</button>
</div>
<button type="button" class="next" name="button" ng-click="addColumn()">+</button>
JS:
app.controller('CreateRecipeController', ['$scope', '$location', '$routeParams', 'DashFactory', function($scope, $location, $routeParams, DashFactory){
console.log("entered Create Recipe controller");
var columnCount = 1;
$scope.addColumn = function addColumn(){
columnCount++;
console.log('attempting to create column');
var d = document.createElement("div");
d.className = "inputField";
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");
var d2 = document.createElement("div");
d2.className = "form-group"
var i = document.createElement("input"); //input element, text
i.setAttribute('type',"number");
i.setAttribute('ng-model',"recipe.on"+columnCount);
i.setAttribute('value',"");
var d3 = document.createElement("div");
d3.className = "form-group"
var s = document.createElement("input"); //input element, Submit button
s.setAttribute('type',"number");
s.setAttribute('ng-model',"recipe.oz"+columnCount);
s.setAttribute('value',"");
s.setAttribute('readonly','readonly')
var d4 = document.createElement("div");
d4.className = "form-group"
var t = document.createElement("input"); //input element, Submit button
t.setAttribute('type',"number");
t.setAttribute('ng-model',"recipe.off"+columnCount);
t.setAttribute('value',"");
d.appendChild(f);
f.appendChild(d2);
f.appendChild(d3);
f.appendChild(d4);
d2.appendChild(i);
d3.appendChild(s);
d4.appendChild(t)
document.getElementsByClassName('timingSelect')[0].appendChild(d);
}
$scope.createRecipe = function(recipe){
console.log('recieved recipe data', recipe);
DashFactory.createRecipe(recipe)
}
}
]);
Great - Just move all your buttons inside your form tag like the code below
<div class="timing">
<form class="timingSelect" action="index.html" method="post">
<div class="inputField">
<div class="form-group">
On: <input type="number" ng-model="recipe.on1" value="" step=".1">
</div>
<div class="form-group">
oz: <input type="number" ng-model="recipe.oz1" value="" readonly="readonly">
</div>
<div class="form-group">
Off: <input type="number" ng-model="recipe.off1" value="" step='.1'>
</div>
</div>
<button type="submit" ng-click="createRecipe(recipe)">Submit</button>
<button type="button" class="next" name="button" ng-click="addColumn()">+</button>
</form>
</div>
And don't add another form when you append a new input field just add all your new inputs inside the existing form and try to submit it again - This might work
Thanks - Happy Coding !!
I suppose this should be straightforward, but I'm stuck to get that "cpsc" value. I tried to google this, and almost all of the search results told me to use ".value". But this particular class "control term" doesn't seem to work, and only returned "undefined".
<div class="control term">
<input type="text" value="cpsc">
</div>
My code:
document.getElementsByClassName("control term")[0].value;
You are reading from the div. You should read from input instead.
<div class="control term">
<input id="inputTag" type="text" value="cpsc">
</div>
document.getElementsBYId("inputTag").value;
The problem is that the class "control term" is on the div element and not your input. Also as a note, a class should be only one word (ie. controlTerm), having a space between them assigns two different classes to the div: control, and, term.
You have two options:
Add a class to your input
Get the child of document.getElementsByClassName("control term") and then extract its value.
Hope this helps
<div class="control">
<input class="term" type="text" value="cpsc">
</div>
document.getElementsByClassName("term")[0].value;
<div>
<input type="text" value="cpsc" class="control term">
</div>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert(document.getElementsByClassName("control term")[0].value);
}
</script>
You have to put class attribute in input tag
You need to add class to your input and then you can get the value of input using the following code.
<div class="control term">
<input type="text" class = "test" value="cpsc">
</div>
<button onclick = "myFunction()">Click me</button>
<script>
function myFunction(){
alert(document.getElementsByClassName("test")[0].value);
}
</script>
If you have multiple input box with same class name inside "control term" div, you can access their values using following code.
<div class="control term">
<input type="text" class = "test" value="cpsc">
<input type="text" class = "test" value="cpsc1">
</div>
<button onclick = "myFunction()">Click me</button>
<script>
function myFunction(){
alert(document.getElementsByClassName("test")[0].value);
alert(document.getElementsByClassName("test")[1].value);
}
</script>
Or if you want to get all values of input box inside "control term" div, you can do something like this.
<div class="control term">
<input type="text" class = "test" value="cpsc">
<input type="text" class = "test" value="cpsc1">
</div>
<button onclick = "myFunction()">Click me</button>
<script>
function myFunction(){
var x = document.getElementsByClassName("control term");
for(var i = 0;i<=x.length;i++){
alert(x[0].getElementsByClassName("test")[i].value);
}
</script>
Hope this will help you.
i have a bootstrap site and have 4 different forms. I set the required attribute against certain fields. On my local machine, the validation works if the fields are left blank but when I post it to a web host, the validation does not work.
The field types range from free text input to check boxes, select boxes and radio buttons.
I am writing this from my mobile so don't have a snippet of code to show. Sorry all. Suggestions are more than welcome.
<fieldset>
<b><u><h4>Trip Type</h4></u></b>
<div class = "form-group">
<label for="where">Will you be:</label>
<select class="form-control" id="where" name = "where" required>
<option></option>
<option value = "travelling to">Travelling to the Airport</option>
<option value = "travelling from">Travelling from the Airport</option>
</select>
</div>
<div class = "row">
<div class = "col-lg-6 col-md-6">
<div class = "form-group">
<label for = "airporttype">From / to which airport:</label>
<div class = "radio">
<label class="radio-inline"><input type="radio" name="airporttype" id = "airporttype" value = "International" required>International</label>
<label class="radio-inline"><input type="radio" name="airporttype" id = "airporttype" value = "Domestic">Domestic</label>
</div>
</div>
</div>
<div class = "col-lg-6 col-md-6">
<div class="control-group form-group">
<label for = "trip">Is this a Single or Return Trip:</label>
<div class = "radio">
<label class="radio-inline"><input type = "radio" name = "trip" id = "trip" class = "return" value = "Single" required>Single</label>
<label class="radio-inline"><input type = "radio" name = "trip" id = "trip" class = "return" value = "Return">Return Trip</label>
</div>
</div>
</div>
</div>
<hr>
<b><u><h4>Your Details</h4></u></b>
<div class="control-group form-group">
<div class="controls">
<label for = "fname">First Name:</label>
<input type = "text" class = "form-control" id = "fname" name = "fname" required placeholder = "Enter First Name" data-validation-required-message="Please enter your first name.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "surname">Last Name:</label>
<input type="text" class="form-control" id="surname" name = "surname" required placeholder = "Enter Last Name" data-validation-required-message="Please enter your last name.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "cell1">Contact Mobile / Cell:</label>
<input type="text" class="form-control" id="cell1" name = "cell1" required placeholder = "Enter Mobile / Cell Number Main" data-validation-required-message="Please enter your main contact number.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "cell2">Contact Mobile / Cell 2:</label>
<input type="text" class="form-control" id="cell2" name = "cell2" placeholder = "Enter Mobile / Cell Number Alt" data-validation-required-message="Please enter your alternate contact number.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "email">Enter your email address:</label>
<input type = "email" class = "form-control" id = "email" name = "email" required placeholder = "you#yourdomain.com" data-validation-required-message="Please enter your email address.">
<p class="help-block"></p>
</div>
</div>
<br>
<div class="control-group form-group">
<div class="controls">
<label for = "date16">Travel Date:</label>
<input type = "text" class="form-control datepicker" id = "date16" name = "date16" required data-validation-required-message="Please enter your travel date." style = "text-align: center">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for="address">Pick Up / Destination Address:</label>
<input type = "text" class = "form-control" id = "address" name = "address" placeholder = "The start or end of your journey" required data-validation-required-message = "Please enter the address where you will start or finish your journey.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "passnum">Number of Passengers:</label>
<select class="form-control" id = "passnum" name = "passnum" required>
<option></option>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "4">4</option>
<option value = "5">5</option>
<option value = "6">6</option>
<option value = "7">7</option>
<option value = "8">8</option>
<option value = "9">9</option>
<option value = "10">10</option>
<option value = "11">11</option>
<option value = "12">12</option>
<option value = "13">13</option>
<option value = "14">14</option>
<option value = "15">15</option>
<option value = "16">16</option>
<option value = "17">17</option>
<option value = "18">18</option>
<option value = "19">19</option>
<option value = "20">20</option>
<option value = "21">21</option>
<option value = "22">22</option>
<option value = "23">23</option>
<option value = "24">24</option>
</select>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "flightnum">Flight Number:</label>
<input type = "text" class="form-control" id = "flightnum" name = "flightnum" placeholder = "Please enter your flight number" required data-validation-required-message = "Please enter your flight number.">
<p class="help-block"></p>
</div>
</div>
<hr>
<b><u><h4>Child Seats</h4></u></b>
<div class = "row">
<div class = "col-lg-4">
<div class="control-group form-group">
<label for = "babyseat">Baby Seat Required:</label>
<div class = "radio">
<label class="radio-inline"><input type = "radio" name = "babyseat" id = "babyseat" class = "bseat12" value = "Yes" required>Yes</label>
<label class="radio-inline"><input type = "radio" name = "babyseat" id = "babyseat" class = "bseat12" value = "No">No</label>
</div>
</div>
<div class="control-group form-group" id = "babyseat1">
<div class="controls">
<label for = "bseatnum">Number of Baby Seats:</label>
<select class="form-control" id = "bseatnum" name = "bseatnum">
<option></option>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "4">4</option>
<option value = "5">5</option>
</select>
</div>
</div>
</div>
<div class = "col-lg-4">
<div class="control-group form-group">
<label for = "boost">Booster Seat Required:</label>
<div class = "radio">
<label class="radio-inline"><input type = "radio" name = "boost" id = "boost" class = "boost12" value = "Yes" required>Yes</label>
<label class="radio-inline"><input type = "radio" name = "boost" id = "boost" class = "boost12" value = "No">No</label>
</div>
</div>
<div class="control-group form-group" id = "boost1" >
<div class="controls">
<label for = "boostnum">Number of Booster Seats:</label>
<select class="form-control" id = "boostnum" name = "boostnum">
<option></option>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "4">4</option>
<option value = "5">5</option>
</select>
</div>
</div>
</div>
<div class = "col-lg-4">
<div class="control-group form-group">
<label for = "cradle">Baby Cradle Required</label>
<div class = "radio">
<label class="radio-inline"><input type = "radio" name = "cradle" id = "cradle" class = "cradle12" value = "Yes" required>Yes</label>
<label class="radio-inline"><input type = "radio" name = "cradle" id = "cradle" class = "cradle12" value = "No">No</label>
</div>
</div>
<div class="control-group form-group" id = "cradle1">
<div class="controls">
<label for = "cradlenum">Number of cradles:</label>
<select class="form-control" id = "cradlenum" name = "cradlenum">
<option></option>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "4">4</option>
<option value = "5">5</option>
</select>
</div>
</div>
</div>
</div>
<hr>
<b><u><h4>Additional Services</h4></u></b>
<div class = "row">
<div class = "col-lg-6">
<div class="control-group form-group">
<label for = "shuttle">Shuttle Service:</label>
<div class="radio">
<label for = "shuttle" class="radio-inline"><input id = "shuttle" name = "shuttle" type="radio" value = "Yes" required>Yes</label>
<label for = "shuttle" class="radio-inline"><input id = "shuttle" name = "shuttle" type="radio" value = "No">No</label>
<p class="help-block"></p>
</div>
</div>
</div>
<div class = "col-lg-6">
<div class="control-group form-group">
<label for "charter">Private Charter:</label>
<div class="radio">
<label class="radio-inline"><input type="radio" name="charter" id = "charter" value = "Yes" required>Yes</label>
<label class="radio-inline"><input type="radio" name="charter" id = "charter" value = "No">No</label>
<p class="help-block"></p>
</div>
</div>
</div>
</div>
<hr>
</fieldset>
validation only using required attribute is not good, because it may not work in many cases. for example when using javascript submit() method, also in some browsers,required wont work. So Best way is to validate using javascript(client side)
So create a javascript function and call it during form onsubmit event
function checkform(form) {
var inputs = form.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
if(inputs[i].hasAttribute("required")){
if(inputs[i].value == ""){
alert("Please fill all required fields");
return false;
}
}
}
return true;
}