admin管理员组文章数量:1414614
I want to add a class to a li with specific Id. I tried this:
function updateIndex(indexValue){
$('li[id=' + indexValue + ']').addClass("selectedQuestion")
}
I am not able to access indexValue variable in the li selector.
I want to add a class to a li with specific Id. I tried this:
function updateIndex(indexValue){
$('li[id=' + indexValue + ']').addClass("selectedQuestion")
}
I am not able to access indexValue variable in the li selector.
Share Improve this question edited Jun 23, 2015 at 12:16 Vivek Sadh asked Jun 23, 2015 at 12:10 Vivek SadhVivek Sadh 4,2683 gold badges35 silver badges54 bronze badges3 Answers
Reset to default 6You need to do a string concatenation to use the value of the indexValue
parameter:
$('li[id=' + indexValue + ']').addClass("selectedQuestion")
Or, given the attribute you are selecting by is an id:
$('li#' + indexValue).addClass("selectedQuestion");
Or, given that id is supposed to be unique you shouldn't need the 'li' part (unless your meaning is to select that element only if it has that id and is an li element):
$('#' + indexValue).addClass("selectedQuestion");
function updateIndex(index){
$('li[id=' + index +']').addClass("selectedQuestion")
}
Use this code instead of yours. You have to concatinate the indexValue variable
function updateIndex(indexValue){
$('li[id=' + indexValue + ']').addClass("selectedQuestion")
}
本文标签: javascriptAdd class to li with specific IDStack Overflow
版权声明:本文标题:javascript - Add class to li with specific ID - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745181890a2646503.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论