0

I want to apply css to dynamic div id.

var status = var status = item.down().next().innerHtml();
if(status == "test")
{  
    var c = 'item_'+i ;
    c.style.backgroundColor = 'rgb(255, 125, 115)';
    //'item'+ i.style.backgroundColor = 'rgb(255, 125, 115)';
}  

here "item_" + i is a dynamic Ids of every rows.like item_1,item_2,item_3 etc. So I want to add css in some of the rows. ie . item_1 and item_3 or else.

So how can this possible.

4 Answers 4

1

You are trying to apply the style property to string instead you need to get the element by id. Use documet.getElementById() for that

var status = var status = item.down().next().innerHtml();
if(status == "test")
{  
    var c = 'item_'+i ;
    document.getElementById(c).style.backgroundColor = 'rgb(255, 125, 115)';
    //'item'+ i.style.backgroundColor = 'rgb(255, 125, 115)';
} 

Or use jQuery id-selector and for applying css use css()

var status = var status = item.down().next().innerHtml();
if(status == "test")
{  
    var c = 'item_'+i ;
    $('#'+c).css('background-color','rgb(255, 125, 115)');
    //'item'+ i.style.backgroundColor = 'rgb(255, 125, 115)';
} 
Sign up to request clarification or add additional context in comments.

Comments

0

You can use as mentioned other users, or you can also use the below, basically if you use pure JavaScript then you have to identify the element first by using the function document.getElementbyId

var c = document.getElementbyId('item_' + i);
c.style.backgroundColor = 'rgb(255, 125, 115)';

Comments

0

There are many ways for achieving result you want.In addition to others answers you can also do like this with jquery

$("div#"+dynamic_id).css("background-color", "rgb(255, 125, 115)");

Comments

0

In order to add CSS to dynamic div you can use the following syntax

$("div"+dynamic_id).css({"color":"rgb(255, 125, 115)"});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.