admin管理员组文章数量:1389903
I am having 2 text box.. Value of first text box should been an length of second text box.. Eg: If user gives First text box value as "10", then my second text box should not allow user to type more than 10 characters..
Here is my code..
function field_length() {
var fieldValue = document.getElementById('Length').value;
alert(fieldValue);
}
<input type="text" name="Length[]" maxlength="2" class="required" id="Length" onkeypress="return isNumberKey(event);" placeholder="Field length" class="form-control">
<input type="text" name="Label[]" class="required" id="Label" maxlength="" onClick="field_length();" placeholder="Field Label" class="form-control">
I am having 2 text box.. Value of first text box should been an length of second text box.. Eg: If user gives First text box value as "10", then my second text box should not allow user to type more than 10 characters..
Here is my code..
function field_length() {
var fieldValue = document.getElementById('Length').value;
alert(fieldValue);
}
<input type="text" name="Length[]" maxlength="2" class="required" id="Length" onkeypress="return isNumberKey(event);" placeholder="Field length" class="form-control">
<input type="text" name="Label[]" class="required" id="Label" maxlength="" onClick="field_length();" placeholder="Field Label" class="form-control">
In this code what i did was.. if user is gives value for first field as "5", on tap of second field it will alert the value.. But i want that value to be assigned to Maxlenght attribute. Give me some idea..
Share edited Oct 20, 2021 at 16:54 connexo 56.9k15 gold badges111 silver badges146 bronze badges asked Oct 27, 2016 at 5:57 NithyaNithya 1,1213 gold badges16 silver badges27 bronze badges 8- try this document.getElementById("yourID").maxLength .. L should be capital. And please do some research before asking such easy questions as there are already so many posts available if you google it .. thanks – Mittul At TechnoBrave Commented Oct 27, 2016 at 5:58
-
change
.value
to.maxlength
– user6360214 Commented Oct 27, 2016 at 5:58 -
you should use jquery
$('#Length').attr('maxlength','5')
– Mayank Vadiya Commented Oct 27, 2016 at 6:00 - @MayankVadiya he wants to achieve this using javascript as per what his code suggests .. – Mittul At TechnoBrave Commented Oct 27, 2016 at 6:01
- @MayankVadiya How's jquery performance? – hdotluna Commented Oct 27, 2016 at 6:03
3 Answers
Reset to default 5Get length and set maxLength attribute.
function field_length(){
var length = $("#Length").val();
$("#Label").attr("maxlength", length)
}
You can use setAttribute
<script type="text/javascript">
function field_length()
{
var fieldValue= document.getElementById('Length').value;
document.getElementById("Label").setAttribute('maxlength',fieldValue);
}
</script>
<input type="text" name="Length[]" maxlength="2" class="required" id="Length" onkeypress="return isNumberKey(event);" placeholder="Field length" class="form-control">
<input type="text" name="Label[]" class="required" id="Label" maxlength="" onClick="field_length();" placeholder="Field Label" class="form-control">
Try this:
$("#Label").attr("maxlength", length);
or
$("#Label").prop("maxlength", length);
NOTE:
As of jQuery 1.6. , the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.
本文标签: How to assign javascript variable value to HTML AttributeStack Overflow
版权声明:本文标题:How to assign javascript variable value to HTML Attribute? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744652869a2617790.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论