Reputation: 1351
I have a list of buttons generated dynamically...
var output="";
var active;
var x;
var i;
var user_id=localStorage.get('user_id');#
for(x=0;x<dynamic_count;x++)
{
output+="<div class='tbl' data-role='button' data-table_id='"+(x+1)+"'>";
output+="<p class='center_text'>"+(x+1)+</p>";
output+="<div>";
}
$('.table_holder').html(output).trigger('create');
//active and active_count come from AJAX request (I have missed this bit our of the code..active[0]=a table number where as active[1]= s user_id
for(i=0;i<active_count;i++)
{
if(active[1]==user_id)
{
$('.tbl').find("[data-table_id='"+active[0]+"']").css('backgroundColor', 'red');
}
}
Unfortunately this has no effect on the background color of the desired element. I am not sure whether it is a problem with my selector code, my css code, or a propblem with my implementation of jQuery Mobile.
I have noticed that when dynamically adding elements that need styling with jQuery Mobile I have needed to use the trigger('create')
method to apply the css.
This obviously over writes any amended css with the original jQuery css.
Upvotes: 1
Views: 2509
Reputation: 31732
First, create a custom class e.g. .custom-class
CSS: Note that !important
is essential to override JQM default styles.
.custom-class { background-color: red !important; }
Code:
Find all buttons with [data-table_id]
attribute, compare values and apply .custom-class
var buttons = $(document).find('a[data-table_id]');
$.each(buttons, function () {
$(this).removeClass('custom-class');
if ($(this).attr('data-table_id') == user_id) {
$(this).addClass('custom-class');
}
});
Upvotes: 2
Reputation: 11830
Try this
$('.tbl').find("[data-table_id='"+active[0]+"']").css('background-color', 'red');
you are trying to assign background color like this
$('.tbl').find("[data-table_id='"+active[0]+"']").css('backgroundColor', 'red');
in jquery you need to use background-color instead of backgroundColor
Upvotes: 0