Eg. document.form[0].elements[23] will result some input <input id = test2> .
I need to get that 23 index value by id or name = 'test2' like
var n = document.getElementById('test2').getDOMNodeIndex(); // n = 23
This solution may be slightly clunky, but it works as demonstrated below.
I generated 25 input elements and gave them unique IDs.
Then I selected a random index among the Form.elements and printed it to the console.
Then I iterated over the form.elements until I found the matching .outerHTML values (JSON.stringified). When a match is found, return that index.
const theForm = document.forms[0];
const randomIndex = Math.floor(Math.random() * theForm.elements.length);
console.log(randomIndex);
const theInput = theForm.elements[randomIndex];
const formElementsIndexOf = (form, element) => {
let search = JSON.stringify(element.outerHTML);
for (let i = 0; i < form.elements.length; i++) {
if (JSON.stringify(form.elements[i].outerHTML) === search) {
return i;
}
}
};
console.log(formElementsIndexOf(theForm, theInput));
<form>
<input type="text" id="text0">
<input type="text" id="text1">
<input type="text" id="text2">
<input type="text" id="text3">
<input type="text" id="text4">
<input type="text" id="text5">
<input type="text" id="text6">
<input type="text" id="text7">
<input type="text" id="text8">
<input type="text" id="text9">
<input type="text" id="text10">
<input type="text" id="text11">
<input type="text" id="text12">
<input type="text" id="text13">
<input type="text" id="text14">
<input type="text" id="text15">
<input type="text" id="text16">
<input type="text" id="text17">
<input type="text" id="text18">
<input type="text" id="text19">
<input type="text" id="text20">
<input type="text" id="text21">
<input type="text" id="text22">
<input type="text" id="text23">
<input type="text" id="text24">
</form>
Related
I am making a query to my database, creating an object, and sending it to my HTML via render_template.
The issue I am having is that the HTML is not displaying the information.
Relevant code:
#app.route('/profile', methods=['POST','GET'])
def profile():
if 'sno' in session:
if request.method == 'GET':
user = session['sno']
print(user)
cursor = conn.cursor()
cursor.execute("""SELECT * FROM `users` WHERE `sno` LIKE '{}'.format(user))
r = cursor.fetchall()
print(r)
return render_template('profile.html', r=r)
return redirect('/')
<form class="form" method="POST" action="/edit" name="Name">
<label>First Name</label><br>
<input type="text" class="form-control" name="fname" value="{{r[1]}}" >
<label>Last Name</label><br>
<input type="text" class="form-control" name="lname" value="{{r[2]}}" >
<label>Age</label><br>
<input type="text" class="form-control" name="age" value="{{r[3]}}">
<label>CNIC</label><br>
<input type="text" class="form-control" name="cnic" value="{{r[4]}}">
<label>gender:</label>
<input type="text" name="gender" class="form-control" value="{{r[5]}}"><br>
<label>Email</label><br>
<input type="email" class="form-control" name="uemail" value="{{r[6]}}">
<label>Password</label><br>
<input type="text" name="upassword" class="form-control" value="{{r[7]}}"><br>
<input type="submit" class="btn btn-primary btn-block btn-lg" name="submit" value="Edit">
</form>
Is there something I am missing or doing wrong?
I just did this
I had kept method = "POST".I changed it to method="GET"
It worked like boom
I am currently trying to get my calculated fields to show a total in a bottom field but in order for that to happen I have to have a value in each field. I am lost on how to get it to work with an empty field being accepted without an error. This is what i have so far:
<script type="text/javascript">
function update()
{
var form = document.forms[0];
var sum = eval(form.x.value)*60 + eval(form.y.value)*110 + eval(form.z.value)*180;
form.sum.value = isNaN(sum) ? "" : sum;
}
</script>
<form name="form1" onsubmit="false">
<fieldset>
<legend>Bloodwood</legend>
<label for="f1">Vault ($60)</label>
<input id="f1" name="x" type="number" onchange="update()">
<label for="f2">Tray ($110)</label>
<input id="f2" name="y" type="number" onchange="update()">
<label for="f3">Tower ($180)</label>
<input id="f3" name="z" type="number" onchange="update()">
<label for="f4">Total</label>
<input id="f4" name="sum" readonly="readonly">
</fieldset>
</form>
I have removed eval from the sum and it works on tab out
function update()
{
var form = document.forms[0];
var sum = form.x.value*60 + form.y.value*110 + form.z.value*180;
form.sum.value = isNaN(sum) ? "" : sum;
}
<form name="form1" onsubmit="false">
<fieldset>
<legend>Bloodwood</legend>
<label for="f1">Vault ($60)</label>
<input id="f1" name="x" type="number" onchange="update()">
<label for="f2">Tray ($110)</label>
<input id="f2" name="y" type="number" onchange="update()">
<label for="f3">Tower ($180)</label>
<input id="f3" name="z" type="number" onchange="update()">
<label for="f4">Total</label>
<input id="f4" name="sum" readonly="readonly">
</fieldset>
</form>
I am trying to specify a dynamic url in the actions properties of form.
The url will be dependant on what the user enters in the textbox. For example, if the user enters "Fred" as firstname and 1234 as courseId, then the url should be "/users/Fred/course/1234"
This is what I have so far but it is not reading the firstname and courseId.
<form action="/users/{{firstname}}/course/{{courseId}}" method="POST">
<input type="text" placeholder="firstname" name="firstname">
<input type="email" placeholder="email" name="email">
<input type="text" placeholder="courseId" name="courseId">
<input type="submit" value="Submit">
</form>
No php and ajax can be used.
You need javascript to do this
const template = (firstname, courseId) => `/users/${firstname}/course/${courseId}`;
document.getElementById('myForm').addEventListener('submit', function(s) {
s.preventDefault();
this.action = template(this.elements["firstname"].value, this.elements["courseId"].value);
this.submit();
});
<form action="placeholder" method="POST" id="myForm">
<input type="text" placeholder="firstname" name="firstname">
<input type="email" placeholder="email" name="email">
<input type="text" placeholder="courseId" name="courseId">
<input type="submit" value="Submit">
</form>
I have a form that offers a user a choice of whether they would like to receive Email updates. How do I go about making sure that the Email is only 'required' (HTML5 Validation) if the checkbox to receive Email updates is checked.
Here is the form:
<form>
<fieldset>
<legend><b>Details</b></legend>
<label>First Name </label><input id = "fname" type="text" autofocus="" placeholder="Enter first name" name = "fname"><br><br>
<label>Last Name </label><input type="text" placeholder="Enter last name"><br><br>
<label>Email </label><input type="email" placeholder="Enter valid email"> <button onclick="myFunction()">Help</button><br><br>
</fieldset>
<fieldset>
<legend><b>Rating</b></legend>
<label>Website Rating:</label>
<input type="radio" name="Rating" value="1">* (1 Star)
<input type="radio" name="Rating" value="2">* * (2 Star)
<input type="radio" name="Rating" value="3">* * * (3 Star)
<input type="radio" name="Rating" value="4">* * * * (4 Star)
<input type="radio" name="Rating" value="5">* * * * * (5 Star)<br>
</fieldset>
<fieldset>
<legend><b>Comments</b></legend>
<label>Additional Comments:</label>
<textarea id = "feedback1" name="feedback1" rows="2" cols="60"></textarea><br>
</fieldset>
<fieldset>
<legend><b>Updates</b></legend>
Do you want to receive updates via Email?<br>
<input type="checkbox" name="updateYes" value="Yes">Yes
<input type="checkbox" name="update" value="No" checked>No<br>
</fieldset>
<br>
<input type="reset" value="Reset">
<button onclick="myFunction2()" type = "submit">Submit</button>
</form>
<script>
function myFunction() {
alert("Please enter a valid Email adress into the 'Email' field");
}
function myFunction2() {
var checkedRadioButton, inputs, rating;
inputs = document.getElementsByName("Rating");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
checkedRadioButton = inputs[i];
break;
}
}
if (checkedRadioButton) {
rating = ("You have rated the website " + checkedRadioButton.value + " Star")
} else {
rating = ("Not Rated")
}
alert("Thank you for your feedback " + document.getElementById('fname').value + "\n" + rating + "\n" + "Your comment was " + document.getElementById('feedback1').value);
}
</script>
I want to clear my input values in html form when you press submit.
the function is in the controller.
i tried to do: $scope.name = "" or null (for every input) but its don't work.
This is the html file:
<label for="messageName">Message Name:</label>
<input type="text" ng-model="message.name" name="message.name" required/>
<label for="text">Text:</label>
<input type="textarea" ng-model="message.text" name="message.text" required/>
<label for="images">Images:</label>
<input type="text" ng-model="message.images" name="message.images" required/>
<label for="template">Template:</label>
<input list="template" ng-model="message.template" name="message.template" required/>
<datalist id="template">
<option value="template1">
<option value="template2">
<option value="template3">
</datalist>
<label for="millisecToShow">Millisec To Show:</label>
<input type="number" min="1000" max="10000" ng-model="message.millisecToShow" name="message.millisecToShow" required/>
<label for="timeFrame">Time Frame:</label>
<label for="TimeStart">Start Time:</label>
<input type="datetime-local" ng-model="message.startDateTime" name="message.startDateTime" required/>
<label for="DateStart">End Time:</label>
<input type="datetime-local" ng-model="message.endDateTime" name="message.endDateTime" required/>
<label for="days">Days:</label><br>
<form>
<input type="checkbox" ng-model="message.sunday" name="message.days" value="Sunday">Sunday<br>
<input type="checkbox" ng-model="message.monday" name="message.days" value="Monday">Monday<br>
<input type="checkbox" ng-model="message.tuesday" name="message.days" value="Tuesday">Tuesday<br>
<input type="checkbox" ng-model="message.wednesday" name="message.days" value="Wednesday">Wednesday<br>
<input type="checkbox" ng-model="message.thursday" name="message.days" value="Thursday">Thursday<br>
<input type="checkbox" ng-model="message.friday" name="message.days" value="Friday">Friday<br>
<input type="checkbox" ng-model="message.saturday" name="message.days" value="Saturday">Saturday<br>
</form>
<label for="screenId">Screen ID:</label>
<input list="screenId" ng-model="message.screenId" name="message.screenId" required/>
<datalist id="screenId">
<option value="screen1">
<option value="screen2">
<option value="screen3">
</datalist>
<button ng-click="createButton()">Create</button>
And this is the controller:
angular.module('CreateCtrl', []).controller('CreateController', function($scope, superService) {
$scope.createButton = function() {
superService.insert($scope.message).then(function (result)
if (result == "true") {
alert('Create successful');
$scope.tagline = 'Create successful';
return result.data;
}
else {
$scope.tagline = 'Cannot create message';
}
});
};
});
I want to remove the input after sending the file.
thanks (:
Considering that all of the fields refer to $scope.message.*, wouldn't it be easier to just clean up the whole message object like this?
if (result === "true") {
alert('Create successful');
$scope.tagline = 'Create successful';
// cleaning up
$scope.message = {
name: null,
text: null,
images: null,
template: null
};
return result.data;
}
In fact, if you want to wipe the whole object clean, if it were me I would just go for $scope.message = {}; and be done with it.