Ways to dynamically change the background color

I'm currently developing an Android app using PhoneGap and I need assistance with dynamically changing the background color. Can someone guide me on how to achieve this?

Here is the HTML5 code snippet:

<div data-role="page">
    <div data-role="content" >
        <div class="my_body1">      
            <ul id="table_list_id"></ul>
        </div>
    </div>
</div>

And here is the CSS3 code snippet:

.my_body1 ul li {
    float: left;
    list-style-type: none;
    border: 2px solid #a1a1a1;
    padding: 5px 5px;
    text-align: center;
    width: 120px;
    border-radius: 5px;
    margin: 1%;
    box-shadow: 5px 5px 5px #888888;
}

.my_body1 ul {
    width: 100%;
}

.my_body1 ul li a {
    text-decoration: none;
    color: #525252;
}

Also, in jQuery:

function callXMLConnection() {
    $("#table_list_id").empty();
    $.support.cors = true;
    $.ajax({
        type: "GET",
        url: "table.html",
        contentType: "text/xml",
        dataType: "xml",
        data: "",
        cache: false,
        processData: false,
        crossDomain: true,
        success: function (data) {
            $(data).find('xyz').each(function () {
                var title = $(this).find('title').text();
                var status = $(this).find('status').text();
                if (status == 'vacant') {
                    var scripts = "<li><a href='#'>" + title + "</a></li>"
                    $("#table_list_id").append(scripts).trigger('create');
                }
                else if (status == 'occupied') {
                    var scripts = "<li><a href='#' >" + title + "</a></li>"
                    $("#table_list_id").append(scripts).trigger('create');
                }
            });
        }

    });
}
$(document).unbind('pageinit').bind('pageinit', function () {
    callXMLConnection();
});

If the status is vacant, I'd like the background color to be green. If the status is occupied, then it should be red. Any help would be greatly appreciated!

Answer №1

If you're looking to change the background color with some jQuery magic, this snippet of code could come in handy.

$("#container_div_id").css("background-color","YOUR DESIRED COLOR")

Answer №2

If you want to change the background color using jQuery and set it as a CSS property, simply utilize this code snippet:

$("#element_id_here").css("background-color", "YOUR DESIRED COLOR")

Answer №3

Consider utilizing css in this manner:

var styles='';
if(state == 'available'){              
    styles = "<li style='background-color:green'><a href='#'>"+name+"</a></li>";
}  
else if(state == 'booked'){  
    styles = "<li style='background-color:red'><a href='#'>"+name+"</a></li>";
}
if(styles){
   $("#table_list_id").append(styles)
                      .trigger('create');
}

Alternatively, you could define a class named state as follows:

CSS

.available{background-color:green} /* green background for available class */
.booked{background-color:red} /* red background for booked class */

JAVASCRIPT

    ......
    var state = $(this).find('status').text();
    var styles = "<li class='"+state+"'><a href='#'>" + name + "</a></li>";
    $("#table_list_id").append(styles).trigger('create');

Keeping it simple and effective

Answer №4

Thanks so much for your assistance.

Below is the solution I came up with using jQuery:

$(data).find('xyz').each(function () {
            var title = $(this).find('title').text();
            var status = $(this).find('status').text();
            if (status == 'vacant') {
                var code = "<li style='background:#00FF66'><a href='#'>" + title + "</a></li>"
                $("#table_list_id").append(code).trigger('create');
            }
            else if (status == 'occupied') {
                var code = "<li style='background:#FF0000'><a href='#' >" + title + "</a></li>"
                $("#table_list_id").append(code).trigger('create');
            }
}); 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method to activate or deactivate a link with jQuery?

Can you provide guidance on how to toggle the activation of an anchor element using jQuery? ...

Maximize Efficiency with Bootstrap 4: Aligning Content in Flexbox Elements

I'm currently working with Bootstrap 4 and Flexbox to ensure that the columns inside a row are properly aligned. One of my requirements is to have all columns be of the same height, so I utilize the "align-items-stretch" class () to achieve this unifo ...

Establishing connections between check boxes and text entry fields and dynamically updating the data within them

I am attempting to connect a checkbox with a text input so that the value inside the input field can be updated. The issue I'm facing is how to appropriately update the value when the checkbox is checked. For instance, my goal is to display the status ...

Ways to generate multiple elements using JavaScript

Is there a way to dynamically freeze columns in a table as I scroll it horizontally? I've achieved this statically using JavaScript, but is there a way to indicate the number of columns and achieve the desired style? This is what my JavaScript code c ...

Please upgrade Cordova to the latest version 3.5.0 for improved functionality

I currently have Cordova installed in my project with version 3.4.0-0.1.3 and I am attempting to upgrade it to version 3.5.0-0.2.7. I have executed the following command: sudo npm update -g [email protected] This command runs successfully without any i ...

Applying the fadeIn() function within the afterAdd callback of KnockoutJS

I'm currently delving into the world of the foreach binding, but I'm stumped as to why the $(element).fadeIn(500) line in the code snippet below isn't functioning properly: ko.applyBindings({ myItems: ko.observableArray([ 'A& ...

What could be the reason my spin animation is only triggered once upon button press?

Every time the submit button is clicked, I want all the images to spin. However, this rotation effect only works the first time the button is pressed, until the page is refreshed. I have used jQuery to add and remove a class from the images when the button ...

Modify the static navigation menu to a dynamic menu in Wordpress

Currently, I am attempting to transform my static navigation menu into a dynamic WordPress navigation menu. This is the code that I currently have: <nav> <ul id="menu"> <?php $pages = array( 'in ...

Double firing of Jquery trigger event

Check out this example I have, where the trigger fires twice when selecting cars. Take a look at my code: $(document).on('change','.sellkop',function(){ if ($("#rs").is(':checked')) { $("#text_container").a ...

Implementing a dropdown login feature in HTML

Is there a way I can make these dropdown forms slide back up when I click anywhere outside of them? Below is the javascript code I am using: $(document).ready( function(){ $('#login-trigger').click( function(){ $(this).next('#login-cont ...

Utilize a text entry field and button to search a MySQL database and then forward the results to a

Hi, I have a search box on my masterpage that contains a textbox (TextBox1.Text) and a button (Button1_Click), both are asp controls. I am attempting to search my database for similar items. Specifically, I have a User table that I want to search for User ...

Responsive Bootstrap navigation bar with a prominent logo

I am currently working on creating a navigation bar with logo following two different layouts. The first layout should be centered vertically and look like this on desktop: Desktop Layout For mobile devices, the layout should resemble this design: Mobile ...

Refresh an HTML table dynamically without the need to reload the page or manually click a button by leveraging

As a beginner in PHP, I am looking to update the HTML table automatically when inserting a list of data using SQL commands in XAMPP. This is the code snippet from `index.php` that retrieves data from the database and displays it in the table. `<?php ...

Scrolling to the Bottom with jQuery

I have been looking for a solution on this website, but the only thing I could find is how to use jQuery to scroll to the top. Is there a way to scroll to a specific link or bookmark at the bottom of the page? For example, if I click on a button: <a hr ...

Is there a way to retrieve information from an external URL in JSON format and display it using JavaScript on a separate HTML webpage?

I'm trying to display the value TotalPostNotificationCount from a JSON file in an external HTML using either JavaScript or PHP. The JSON data is as follows: { "DayPostCount": 1, "TotalPostNotificationCount": 7381 } This data can be found at t ...

Achieve a full-height sidebar design similar to Wordpress using flexbox functionality

Working on a web app that features a tools sidebar with a fixed width requirement amidst fluid content. Explored using flexbox to achieve this, but encountered an issue where the main box only takes the height of the viewport instead of the entire site&ap ...

When it comes to printing, the @Media CSS rule

I have a specific structure on my HTML page: <html> <head></head> <body> <nav> .... navigation menu </nav> <div> <div></div> <div class="to-print"> <h2>Th ...

Some browsers are failing to display the navigation bar on my website

After working on my website, www.alexanderpopov.org, I encountered an issue with the navigation bar disappearing on some computers and browsers. I'm using css to style it, but the problem persists. Below is the HTML code for your reference. Any advice ...

Using a combination of HTML and JavaScript to verify the accuracy of the password entered

I'm in the process of creating a website where users can input a code and be redirected to a specific page. It's crucial that each unique code leads the user to a different destination. For example, code 123 should take them to index.html while ...

Having trouble with the Tooltip feature in Bootstrap versions 5.2 and 5.3 on my end

I've been working on building an HTML website using Bootstrap 5.2, and I followed the instructions in the Bootstrap documentation to add tooltips. However, I encountered an issue where the tooltips were not functioning as expected. When checking the ...