javascript - Add class to li with specific ID - Stack Overflow

I want to add a class to a li with specific Id. I tried this:function updateIndex(indexValue){$('l

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 badges
Add a ment  | 

3 Answers 3

Reset to default 6

You 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")

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745181891a4615456.html

相关推荐

  • javascript - Add class to li with specific ID - Stack Overflow

    I want to add a class to a li with specific Id. I tried this:function updateIndex(indexValue){$('l

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信