admin管理员组文章数量:1277290
hi I have a li tag as follows:
<li value="myLi" class="myLi">My element</li>
I tried this jquery code:
$(".myLi").click(function(){
document.getElementById("mytext").value=$(this).value;
});
knowing that mytext
is an input of type text, how can I pass the value of the li tag to the input , if it's not possible, how can I use the innerHTML to pass the code between the li tags.
the code that I tried does not work thank you
hi I have a li tag as follows:
<li value="myLi" class="myLi">My element</li>
I tried this jquery code:
$(".myLi").click(function(){
document.getElementById("mytext").value=$(this).value;
});
knowing that mytext
is an input of type text, how can I pass the value of the li tag to the input , if it's not possible, how can I use the innerHTML to pass the code between the li tags.
the code that I tried does not work thank you
Share Improve this question edited Dec 10, 2010 at 22:07 Brad Mace 27.9k18 gold badges109 silver badges152 bronze badges asked Dec 10, 2010 at 22:05 SmootQSmootQ 2,1227 gold badges35 silver badges59 bronze badges 05 Answers
Reset to default 3Despite what some have said, value
is indeed a valid attribute for li
elements, and it's reflected in the value
property, but it's required to be an integer (reference):
The value attribute, if present, must be a valid integer giving the ordinal value of the list item.
...
If the value attribute is present, user agents must parse it as an integer, in order to determine the attribute's value. If the attribute's value cannot be converted to a number, the attribute must be treated as if it was absent. The attribute has no default value.
If your pasted code was just a quick edit and you really will be using an integer, then your code would work if you didn't wrap this
in a jQuery instance:
HTML:
<li value="3" class="myLi">My element</li>
JavaScript using jQuery:
$(".myLi").click(function(){
document.getElementById('myText').value=this.value;
// Or
// $('#myText').attr('value', this.value);
});
If you want to use a string, probably best to stick with the new HTML5 data-
prefix:
HTML:
<li data-value="myLi" class="myLi">My element</li>
JavaScript using jQuery:
$(".myLi").click(function(){
document.getElementById('myText').value=$(this).attr('data-value');
// Or
// $('#myText').attr('value', $(this).attr('data-value'));
});
AFAIK (Not entirely true, see @T.J. Crowder's answer)value
is not valid attribute for li
elements.
You can use rel
though:
<li rel="myLi" class="myLi">My element</li>
And to set the value of the input element, use val()
:
$(".myLi").click(function(){
$("#mytext").val($(this).attr('rel'));
});
If you want to get the text from the li
element instead, use $(this).text()
.
Reference: attr
, text
Try the following
$(".myLi").click(function() {
$("#myText").val($(this).text());
});
I think what you want is this:
$(".myLi").click(function(){
$('#mytext').val($(this).text());
});
Here's a demo of that: http://jsfiddle/Ender/QdWxH/
Or if you wanted the val from that value attribute: As Felix points out, value isn't a valid attribute of (See @T.J. Crowder's answer) You could change that to <li>
.data-value
, like so:
<li data-value="myLi" class="myLi">My element</li>
Then do this:
$(".myLi").click(function(){
$('#mytext').val($(this).data('value'));
});
Demo of that: http://jsfiddle/Ender/BHrmR/
You can use global attribute 'title' for quick and easy.
<li title="myLi" class="myLi">My element</li>
then
$(".myLi").click(function(){
document.getElementById('myText').value=this.attr('title');
});
Hope this help.
本文标签: javascriptPass a li tag value to a text inputStack Overflow
版权声明:本文标题:javascript - Pass a li tag value to a text input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741282932a2370112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论