admin管理员组文章数量:1391860
I have a li
in a ul like following.
<li class="list-group-item" data_id="1909" id="1909"><input type="checkbox" class="checkboxes" value="1909" name="Navigate Window">Navigate Window</li>
I tried to change a text of this li
with the following code.
$('#list1 #' + selectedId).text('abcd');
Using this code, the text is changing fine. But it losts the input
inside the li
. How do I prevent the replacing whole li
?
And also I tried with the following in stackoverflow. change just text in li that contains img
And i tried it like following,
$("ul.list-group > li.list-group-item > span.thetext").html('abcd');
My ul
class is list-group
. But i failed. How can do this?
Thanks in Advance!
I have a li
in a ul like following.
<li class="list-group-item" data_id="1909" id="1909"><input type="checkbox" class="checkboxes" value="1909" name="Navigate Window">Navigate Window</li>
I tried to change a text of this li
with the following code.
$('#list1 #' + selectedId).text('abcd');
Using this code, the text is changing fine. But it losts the input
inside the li
. How do I prevent the replacing whole li
?
And also I tried with the following in stackoverflow. change just text in li that contains img
And i tried it like following,
$("ul.list-group > li.list-group-item > span.thetext").html('abcd');
My ul
class is list-group
. But i failed. How can do this?
Thanks in Advance!
Share Improve this question edited May 23, 2017 at 10:33 CommunityBot 11 silver badge asked Sep 17, 2014 at 7:32 codebotcodebot 2,6663 gold badges44 silver badges95 bronze badges2 Answers
Reset to default 3The easiest solution is to wrap the text within a span
then change the contents of the span using a selector like
var selectedId = '1909';
$('#list1 #' + selectedId + ' span').text('abcd');
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="list1">
<li class="list-group-item" data_id="1909" id="1909"><input type="checkbox" class="checkboxes" value="1909" name="Navigate Window"><span>Navigate Window</span></li>
</ul>
If that is not possible, then based on your markup, you need to change the contents of the last child of your li
so
var selectedId = '1909';
$('#list1 #' + selectedId).contents().last().text('abcd');
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="list1">
<li class="list-group-item" data_id="1909" id="1909"><input type="checkbox" class="checkboxes" value="1909" name="Navigate Window"><span>Navigate Window</span></li>
</ul>
Try using .append()
and wrap the text inside span
as shown :
$('#list1 #' + selectedId).append('<span>abcd</span>');
Working Demo
本文标签: javascriptHow to change the text of li in ul without changing it39s other propertiesStack Overflow
版权声明:本文标题:javascript - How to change the text of li in ul without changing it's other properties? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744770460a2624307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论