how to assign value to bootstrap input='date' - javascript

I want assign to input=date a value extract from database,
the html is
<input type="date" class="form-control" name="endDate" id='endDate'>,
the assign javascript is $('#endDate').attr('value','2015-02-02');,
but this cannot work, unless I assign it manually like:
<input type="date" class="form-control" name="endDate" id='endDate' value='2015-02-02'>
Can anyone help me solve this problem?

Related

Angular 8 ng-reflect-model shows correct date value but not reflecting in input

Using
<div class="ui-g-12 ui-md-12">
<input
id="licenseForEdit.issuedDate"
type="date"
name="licenseForEdit.issuedDate"
pInputText
placeholder="License Issued Date"
[(ngModel)]="licenseForEdit.issuedDate"
required
[class.field-error]="form.submitted && !licenseForEdit.issuedDate"
/>
</div>
I get:
<input _ngcontent-atp-c10="" id="licenseForEdit.issuedDate" name="licenseForEdit.issuedDate" pinputtext="" placeholder="License Issued Date" required="" type="date" ng-reflect-required="" ng-reflect-name="licenseForEdit.issuedDate" ng-reflect-model="2019-01-01T00:00:00" class="ng-pristine ng-valid ng-touched">
Yet, the input field remains:
How do I get the Model's value to display in the input box? I suspect that it's a databinding issue, but...
First please check if the format is correct.
Find the reference for MDN here.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date.
In case the value needs to be transformed for display purpose please use pipe.
https://angular.io/guide/pipes

How to access Session value from Angularjs.?

i wants to access session value from angular js.or set it in a text field.But its not working.
<input type="text" id="uname" value="<%=session.getAttribute("username")%>" ng-model="username" readonly="readonly">
when i set the value of normal text field its working, like
<input type="text" id="uname" value="<%=session.getAttribute("username")%>" readonly="readonly">
but after adding ng-model its not working.
How i will do this?Please anybody help me.
It's angular's normal behavior. Try to use
ng-value="'<%=session.getAttribute("username")%>'" which is better
or
ng-init="username = '<%=session.getAttribute("username")%>'"

calculation in angularjs output fails with ng-model

I'm working on an angular project where i perform calculations on the page. In the textfield where i put the final results, when i get it an ng-model that answer fails to load. When i take out the ng-model, the answer appears.
But i want it working while it have an ng-model="total" because i will send the total value to the database. With the below script, it works
<input name="price" type="text" id="price" ng-model="price">
<input name="quantity" type="text" id="quantity"ng-model="price">
<input name="total" type="text" id="total" value="{{.price*quantity}}">
But with this
<input name="price" type="text" id="price" ng-model="price">
<input name="quantity" type="text" id="quantity"ng-model="price">
<input name="total" type="text" id="total" value="{{.price*quantity}}" ng-model="total">
it fails to works. The answer doesn't appear in the textbox
Try this instead:
<input name="price"
type="text"
ng-model="price" string-to-number>
<input name="quantity"
type="text"
ng-model="quantity" string-to-number>
<span>{{ price * quantity }}</span>
I'm not sure why you're trying to put the calculated value into the 3rd input, but if you are, you'll want to use ng-model-options to tell the total ngModel that it's to treat that value as a getter/setter - https://docs.angularjs.org/api/ng/directive/ngModelOptions
Also note the string-to-number directive. You're dealing with strings in the input. So in order for interpolation to work, you may need to add those.
edit
here is a working example of how I think you were trying to allow an override on the calculated value. You can enter another value in the total input and hit enter to see it working. This uses the ng-model-options - http://codepen.io/jusopi/pen/XKQzzv

for input type="number" how to set default value to 0

For a textbox that accept numbers as shown below, how can the default value be set to 0?
<input name="smtpPort" type="number" id="smtpPort" size="30" maxlength="50" class="input-full" tabindex="4" value="{{model.PortNumber}}">
Though the value in DB is null, it still shows 0 in textbox.
Any help would be appreciated.
HTML attribute to set a default value is value:
<input type="number" value="1">
You're setting this default value to null, and nothing (in HTML) can change it to zero (or anything else).
You must fix the value in your database, or in your template engine (something like {{model.PortNumber || 0}}).
At least, you can force your field to be filled with required attribute:
<input type="number" required value="">
<input name="smtpPort" type="number" id="smtpPort" size="30" maxlength="50" class="input-full" tabindex="4" value="{{model.PortNumber || 0}}">
I assume you're using AngularJS.
The correct way should be to fix the value before sending to template. However, if you still insist on doing it in frontend, you can also try using || operator in your AngularJS frontend.
<input name="smtpPort" type="number" id="smtpPort" size="30" maxlength="50" class="input-full" tabindex="4" value="{{model.PortNumber || 0}}">

How to add attribute to HTML element using javascript

Using javascript (preferably not jquery) I'm trying to change the line:
<input type="number" name="price" required="" id="id_price">
into
<input type="number" name="price" required="" id="id_price" step="any">
I know the solution's got to be easy but I just can't crack it. Help would be much appreciated!!
As torazaburo suggests in the comment you can do it in one step with setAttribute() method
document.getElementById("id_price").setAttribute("step","any");
<input type="number" name="price" required="" id="id_price">
OR
First create the attribute and set the value. Then add it to the element..
var attr = document.createAttribute('step');
attr.value="any";
document.getElementById("id_price").setAttributeNode(attr);
<input type="number" name="price" required="" id="id_price">

Categories