I have several input in my html (this is one input for exemple) :
<TH>
<FORM>
<input name="designation" type="text" size="12" />
</FORM>
</TH>
So, I want retrieve the text input and show it. So I use this script :
$("#Compil").click(function() {
var n_input = document.getElementById("designation").value;
alert('Designation : ' + n_input); // -> n_input non defini (undefined)
});
But don't work. My alert don't display. Can you help me please?
PS : This a script in Javascript or jQuery please?
You are missing the id attribute on your input, simply change your html to include
<input id="designation" name="designation" type="text" size="12" />
1)in you script you getting the value by id designation but you didn't add id attribute like designation in your html input field .so only its throwing undefined error.
$("#Compil").click(function() {
var n_input = document.getElementById("designation").value;
alert('Designation : ' + n_input);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<TH>
<FORM>
<input name="designation" id="designation" type="text" size="12" />
<input type="submit" name="submit" id="Compil" value="submit" />
</FORM>
</TH>
Related
#for (int i = 0; i < ViewBag.Count; i++) {
<tbody>
<tr>
<td> <input type="text" required readonly name="emp[#i].Total_Marks" value="#ViewBag.Total" class="form-control" id="tm" /></td>
<td> <input type="number" required name="emp[#i].Obtained_Marks" class="form-control" id="ob" /></td>
<td> <input type="number" required name="emp[#i].Percentage" class="form-control" id="percentage" onfocus="calcper()" /></td>
<td> <textarea cols="10" required rows="1" name="emp[#i].Remarks" class="form-control"></textarea></td>*
</tr>
</tbody>
}
function calculatePercentage() {
ab = $("#tm").val();
sb = $("#ob").val();
ntb = parseInt(sb) / parseInt(ab) * 100;
console.log(ntb);
document.getElementById("percentage").value = ntb;
}
I am new to mvc 5 and javascript/jQuery please help me how can I use this jQuery function in percentage in every input type which is created by loop but their id is same
As you said you need to call a function on your percentage textbox.
Let me tell you one thing assigning a same Id to a element is bad practice but you can have same name.
Recommend you to use common class attribute or common name attribute
<input type="text" name="percentage1" id="percentage1" class="percentage">
That you can consider as a empty class which will help you for your script operations
eg:
<html>
<head>
<script>
$('.percentage').keypress(function(){
alert('I called');
});
</script>
</head>
<BODY>
<input type="text" name="percentage1" id="percentage1" class="percentage" />
<input type="text" name="percentage2" id="percentage2" class="percentage"/>
<input type="text" name="percentage3" id="percentage3" class="percentage"/>
</BODY>
</HTML>
In the above code I have three textbox where it is pointed to same function which will show alert box.
In your case just apply the emp[#i] to the Id also and have a common class name or have a common name attribute which will help you.
Happy coding.
I got stuck in a very strange problem need help.
Actually i am trying to assign some value to dynamically generated input fields. So i tried the following code snippet. I mean to say in first step i only need to assign value for first 10 IDs and then again if assigning some other values to rest of IDs then it fails
JavaScript
<script type="text/javascript">
function callAssignMark() {
var rightMarkVal = $("#rightMarkVal").val();
var wrongMarkVal = $("#wrongMarkVal").val();
var initial= $("#from").val();
var last= $("#to").val();
console.log('fromWhere--'+initial);
console.log('to---'+last);
alert('Before Loop');
for (var i = initial; i <= last; i++) {
$('#rightMark_' + i ).val(rightMarkVal);
$('#wrongMark_' + i ).val(wrongMarkVal);
$('#selectData_' + i).prop('checked', 'checked');
}
$("#from").val('');
$("#to").val('');
}
</script>
The above JavaScript code working fine in first attempt means when I assign some value to initial and last variable then the loop iterates and works as expected and assign desired values to dynamically generated input fields, But now the problem is when I assign new value to initial and last variable then the loop not able to iterate any more.
And one more thing I want tell you all that all the dynamic IDs of input fields are generated correctly and I'm also getting the values which is going to be assigned to the input fields on second attempt and my HTML looks like following.
HTML
<div>
<div>
<ul>
<li>
<label>From</label>
<input name="" id="from" type="text" placeholder="from" value="" />
<label>To</label>
<input name="" id="to" type="text" placeholder="to" value="" />
<input name="rightMarkVal" id="rightMarkVal" type="text" placeholder="positive Value" value="" />
<input name="wrongMarkVal" id="wrongMarkVal" type="text" placeholder="Negative value" value="" />
<input type="button" class="btn btn-success" value="Assign Marks" onclick="callAssignMark()">
</li>
</ul>
</div>
<c:set var="counter" value="0" scope="page" />
<div>
<table>
<th>S.No</th>
<th>
<input type="checkbox" id="selectAllCheckBox">
</th>
<th>Right Marks</th>
<th>Wrong Marks</th>
<tbody>
<c:forEach var="list" items="${List}" varStatus="loop">
<tr>
<td>
<c:out value="${counter = counter+1 }" />
</td>
<td>
<form:checkbox path="qId" id="selectData_${counter}" value="${list.id}">
</form:checkbox>
</td>
<td>
<form:input path="rightMark" id="rightMark_${counter}" value="" />
</td>
<td>
<form:input path="wrongMark" id="wrongMark_${counter}" value="" />
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
I am unable to find out where is the problem so please help me.
Any help will be appreciated. Thanks
Im having issues getting Jquery Validation to work properly on dynamically populated fields. First off im using asp.net so I need my name fields to stay the same because on the server side im pulling that array of information splitting it and updating a database.
<form id="insert_additions" action="http://yahoo.com" method="post">
<label>
Name1<input type="text" name="testing" value="" required/>
</label>
<label>
Name2<input type="text" name="box_setup" required value="" />
</label>
<label>
Comments: <input type="text" name="comments" value="" /></label><br />
<div id="work"></div>
<br />
<hr align="left" width="50%"/>
<span style="margin: 0 0 0 300px;">
<input class="submit" type="submit" value="Submit">
<input type="button" id="addbutton" class="flatBtn" value="Add Another Project Line" /></span>
</form>
This here is the code that is adding the fields
var work_count = 2;
$("#addbutton").click(function () { //sets up the validator
//Html to create a new field
var newField = "<hr align='left' width='50%'>\
<label>Name1\
<input type='text' name='testing"+work_count+"' required size='8' value='' \
</label>\
<label>Name2\
<input type='text' name='box_setup' required value='' />\
</label>\
<label>Comments:\
<input type='text' name='comments' value='' />\
</label>\
";
$("#work").before(newField);
$('input[id="testing'+work_count+'"]').rules("add", { // <- apply rule to new field
required: true
});
work_count++;
});
As you can see I have tried adding the validate.rules() with in the javascript so that when it add's the new input's into the page it could also insert new rules. I have tried making the id variable but im not sure what im doing wrong. Validation is only working on the first set of fields. I have read and tried implementing various solutions that I have read on this website but im just lost I have started beating my head against my keyboard. Any help would be great I appreciate and love this web forum.
How about just setting the attribute through jquery?
$('input[name="testing'+work_count+'"]').attr('required', true);
btw, there is no id attribute on your added inputs.
so:
$('input[id="testing'+work_count+'"]').rules("add"
should be
$('input[name="testing'+work_count+'"]').rules("ad
I have two Textfields :
<form action="input.php" method="post">
<!--First Field-->
<input type="text" name="id" id="id" value="" />
<!--Second Field-->
<input type="text" name="name" id="name" value="" />
</form>
Database content :
|ID_STAFF|NAME_STAFF|
|-------------------|
| 1 | Edward |
So, when I input staff name in second field , automatically staff ID will filled in first field.
Example :
Second Field : Edward
First Field : 1
I have use this code, but It only duplicate what I typed in second field
$(function(){
$("#name").keypress(function(){
$("#id").val($(this).val());
});
});
So, what is javascript code that I have to make? Thank You Master :D
You can do it like that.
$(function () {
// Handler for .ready() called.
$("input[type='text']").each(function(){
$(this).attr("id",$(this).attr("name") + "_staff");
});
});
i want to get file name when user click on radio button and replace the file name with the value of radio button. I tried to get file name with the jquery but this wont work.
$('#activeImage').click(function(){
var name = $('input[type=file]').prev("#productImg").val();
alert(name);
})
This is the DOM structure.
<div class="controls">
<input type="file" name="userfile[]" value="" id="productImg" style="border:0px;" />
<input type="radio" name="activeImage" value="" id="activeImage" />
</div>
<div class="controls">
<input type="file" name="userfile[]" value="" id="productImg" style="border:0px;" />
<input type="radio" name="activeImage" value="" id="activeImage" />
</div>
How i get the file name and replace the file name with the value of the radio button
Try using this to get the value of file field
$('#activeImage').click(function(){
var name = $(this).prev('input[type=file]').val();
alert(name);
});
You can rename the file only by using some programming languages.
Try using this.
$('#activeImage').click(function(){
var name = $(this).parents('.controls').find('input[type=file]').val();
alert(name);
});