Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
HTML
<div>
<input ng-model="tripsheet.tripsheet_num" type="text">
</div>
JS
$scope.getTotalTripsheets = function()
{
return $scope.tripsheets.length;
};
i want the above data(getTotalTripsheets) to to default input in the input tag, in the above code i am fetching the length from the database table and tried making it as the default data in the input field.
i tried somthing like this(below), it dos not work, how should i proceed with this?
$scope.tripsheet = function()
{
$scope.tripsheet_num = !$scope.getTotalTripsheets;
};
If you just want a one-time/one-way bind to the length of an Array, use ng-value instead of ng-model:
<input ng-value="tripsheets.length" type="text">
If you have a two-way binding that can be changed from the default:
$scope.tripsheet = {
tripsheet_num: $scope.tripsheets.length
};
<input ng-model="tripsheet.tripsheet_num" type="text">
Example in plunker: http://plnkr.co/edit/UM4lyGZSxB1mEcpy9CqJ?p=preview
Finally, it sounds like you may be doing a remote call to a database, so you could use the following strategy:
$scope.tripsheet = {
$resolved: false,
tripsheet_num: null
};
When the remote call succeeds, set $resolved to true and also set the value of tripsheet_num. In the HTML you could do something like this:
<input ng-disabled="!tripsheet.$resolved" ng-model="tripsheet.tripsheet_num" type="text">
Resolve example on plunker: http://plnkr.co/edit/q2Ua8KGqWr6HDphe92q0?p=preview
you are making you input a model of tripsheet.tripsheet_num which means that on the $scope there is going to be a property called tripsheet.tripsheet_num (I think, not sure because of the . what it will actually do.)
However i do know that when you make the tripsheet num a function it overrides whatever it was before and you should lose the .tripshee_num property. Your structure should be more like this.
$scope.tripsheet = {}
$scope.tripsheet.tripsheet_num = "default value";
if tripsheet is an array then change the model on you input to be "tripsheetLength" and change code to this.
<input ng-model="tripsheetLength" type="text">
$scope.tripsheets = ['info1', 'info2']
$scope.tripsheetLength = $scope.tripsheets.length; // length here is 2
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
Im creating accounts on a api and the password needs those restrictions.The handleRegist() is connecting to the api but the passwords i use always goes through that if cycle.I am using a function to check the password strength for a login the password must contain at least 1 special character 1 uppercase letter and it must be longer than 8 characters.
I have this code so far:
function passwordChecker(password){
var strongPassword = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
if (password.value.match(strongPassword)) {
console.log("Good Password");
handleRegist();
}else
console.log("Try Again!\nnot strong enough");
}
NOTE: the strongPassword variable i found it on the internet and i am not sure if the restriction works like that.
To be able to fire the function you need to call it from somewhere, naturally, you would like to call it inside the onchange event inside the input component.
<input
type="text"
onChange={(e) => passwordChecker({ value: e.target.value })}
/>
Here is a working codesandbox example:
Working example
(open the console from the bottom of the page to see the result while typing inside the input)
Notice: im passing the value inside an object {value: e.target.value}, im doing it because inside your function you are checking password.value.match(strongPassword), it expect to get password.value, therefore i needed to add the value inside an object and pass it to the function.
i recommend to handle the value and store it in a state with useState and pass the state as value to the input.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to make a simple Algebra quiz in HTML and JavaScript, but can't figure out how to make it show up how many answers you got correct. I made it to where the it would turn the correct answer variables into booleans, true is correct and false is incorrect. Then I made if and else statements to see how many answers were correct but the alert (which will be changed) is just displaying zero no matter what. Here is a link to my code.
https://jsbin.com/yivapi/edit?,js
Trenner, I'm not going to be able to answer your question properly without untangling your code, so what I thought I'd do is show you a different (and hopefully easier) way to approach the problem. I hope that's okay.
HTML
Here are the inputs for the questions as I imagine you've set them out. I've only used three here for simplicity. There's a submit button which I'll get to in a moment.
<input id="q1" />
<input id="q2" />
<input id="q3" />
<button id="submit">Submit</button>
JavaScript
First we grab our submit button and assign a function getAnswers to its click event.
var submit = document.getElementById('submit');
submit.onclick = getAnswers;
This is a shortcut function to grab the input values so you don't need to keep writing document.getElementById(id).value later.
function getValue(id) {
return document.getElementById(id).value;
}
OK. So now, instead of having a load of if...else statements we're going to use an object to store all of our information. If you've used other languages they might call something like this a hash map. Basically it's a store of information that has key/value pairs and it's great for storing connected information together. In this case each question is a key which has another object as its value, and that object's keys are value and answer, and it's values are the value of the input element and the actual answer.
function getAnswers() {
// create the object
var quiz = {};
// for each question assign set up a new key (question1)
// and get the input value using our shortcut function.
// We set the actual answer here too.
quiz.question1 = { value: getValue('q1'), answer: '2√19' }
quiz.question2 = { value: getValue('q2'), answer: '√87' }
quiz.question3 = { value: getValue('q3'), answer: '8x√2' }
// Once we've loaded the questions, we can check the answers
// We pass in our quiz object as an argument
checkAnswers(quiz);
}
Here we use a loop to iterate over our quiz object. A simple loop of 7 lines instead of all those horrible if...else statements.
function checkAnswers(quiz) {
// set up out correctAnswers variable
var correctAnswers = 0;
// loop over the quiz object and...
for (var q in quiz) {
// ...for each question we test the input value we've stored
// against the answer also held in that object
// If they match increase the correctAnswers variable
if (quiz[q].value === quiz[q].answer) {
correctAnswers++;
}
}
// finally alert the correctAnswers
alert("You got " + correctAnswers + " out of 15 questions right!");
}
This DEMO will show you the working code. Just add 2√19 into the first input box and press submit and you will get an alert of 1 correct answer(s).
Sorry for not directly answering your question, but I hope you find this code interesting and useful. Good luck with your JavaScript coding.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
JSP PAGE:
<form id="login" action="loginaction">
<input type="text" id="username"/>
<input type="text" id="userpass"/>
<input type="submit"/>
</form>
Basically I want to port a website running on a local sever to mobile. For that i need the mobile application to collect the data from JSON.
You can use jquery's serialize function for that purpose like this:
$('form#login').serialize()
It will create query string which can be handled in back end. Official document can be found here.
If you need actual code for query string to JSON you can use plugin or following function
function QueryStringToJSON(query_string) {
var pairs = query_string.split('&');
var result = {};
pairs.forEach(function(pair) {
pair = pair.split('=');
result[pair[0]] = pair[1];
});
return JSON.parse(JSON.stringify(result));
}
Note: In your html you did not set name attribute for inputs. You should set them in order to get query string.
I would recommend using this jQuery plugin:
https://github.com/marioizquierdo/jquery.serializeJSON
You can use it then like this:
$('form#login').on('submit', function (event) {
event.preventDefault();
var jsonData = $(this).serializeJSON();
console.log('Submitted data in JSON:', jsonData);
return false;
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a java script, getting domain name of webpage like this:
<script>
window.onload = function getDomain()
{
var x = location.hostname;
document.getElementById("domain").innerHTML=x;
document.getElementById("Field25").value=x;
}
</script>
and I have a text box, that I need the x to be its value, like this:
<input id="Field25" name="Field25" type="url" class="field text medium" maxlength="255" tabindex="4" required />
its not working
what should i do?
You're telling it to make a string "x" the value of field25. Just use the variable x.
document.getElementById("Field25").value = x;
Get rid of the window.onload. You've got the syntax wrong anyway. You're trying to do
window.onload = function() {
// Some code here
}
You're currently redefining window.onload as the function getDomain(), then never calling getDomain().
If you get rid of the window.onload completely, the code will do what you want.
Example: http://jsfiddle.net/qLhn8/
Aside: It's possible that window.onload isn't running under the context of jsfiddle, so you may need it after all.
I don't quiet understand, but if you mean that you want the input#Field25 to have the value from the x variable, look closer because you are assigning the string 'x', not the value from the variable x
Update your code to this:
document.getElementById("Field25").value = x;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have the following field,
<input id="department" value="finance"/>
I'm trying to create an object variable containing the field in plain old javascript like so.
var $field = $(document.getElementById("department"));
I'm then trying to pull the value from the field variable, however it's failing.
$field.value;
Does anybody know what I'm doing wrong?
That looks like a jQuery object, which is a collection of DOM elements, and the value property is attached to the DOM elements.
You should have:
var field = document.getElementById("department");
// now you can access field.value
jQuery objects have a val() method that gets you the value of that property:
var field = $('#department');
// here you can call field.val();
Lose the call to $() if you want a DOM element, and not a jQuery object:
$field = document.getElementById("department");