Modifying CSS style according to the contents of an HTML element

Creating a room allocation page with multiple panel boxes using Bootstrap. Each box represents a bed - highlighted in red if occupied and green if vacant. Clicking on a green panel redirects to the room allocation page, while clicking on a red panel goes to the room vacate page. How can I achieve this development efficiently using Bootstrap and JQuery?

function highlightBed(id){
    if($(id).text()=="")
        $(id).prev().parent().prev().css("background-color","green"); 
    else
        $(id).prev().parent().prev().css("background-color","red"); 
}   

//I want to call the same function on page load for multiple ids, currently only able to do so individually on click events.

Answer №1

Several syntax errors were found in the code.

To trigger the event on load for the .roombed class, you can utilize jquery.trigger().

Please refer to the updated jsfiddle link: https://jsfiddle.net/johndoe/example123/

From what I gathered about your issue, it seems that this is the desired outcome:

function triggerFunc(id) {
  if ($("#" + id).text() == "")
    $("#" + id).parent().css("background-color", "green");
  else
    $("#" + id).parent().css("background-color", "red");
}

$(".roombed").trigger("click");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-6">
  <div class="roombedboxB111" style="border:1px solid #FF2D55;width:333px;">
    <div class="roombedhead" style="background-color:green;height:5px;">

    </div>

    <div class="roombed" style="background-color:white;height:116px;" onclick="triggerFunc('allocatedname1');">
      <label style="margin-left: 100px;">BR-111</label>
      <br />
      <label id="allocatedname1">Jon Doe</label>
      <br />
      <label id="allocatedyear">1st Year</label>
      <br />
      <label id="allocatedyear">Computer</label>
    </div>


  </div>
  <div class="roombedboxB111" style="border:1px solid #FF2D55;width:333px;">
    <div class="roombedhead" style="background-color:green;height:5px;">

    </div>

    <div class="roombed" style="background-color:white;height:116px;" onclick="triggerFunc('allocatedname2');">
      <label style="margin-left: 100px;">BR-111</label>
      <br />
      <label id="allocatedname2">Mark Doe</label>
      <br />
      <label id="allocatedyear">1st Year</label>
      <br />
      <label id="allocatedyear">Computer</label>
    </div>


  </div>

</div>

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

JavaScript: A guide to finding CSS properties within a string

Checking for CSS Properties in Over 1,000 Sentences Is there a way in Javascript to check sentences against a built-in CSS index? Currently… If I need to search for CSS properties in the sentences below, I have to create an array containing all the CS ...

Querying a Database to Toggle a Boolean Value with Jquery, Ajax, and Laravel 5.4

I am facing an issue with a button I created to approve a row in a table. It seems that everything is working fine when clicking the button, but there is no change happening in the MySQL database Boolean column. Here is the code for my button: <td> ...

Using SVG Inline Style Conditionals in ReactJS

Would you say I have the conditional inline code set up correctly in this instance? The SVG icon is currently an x sign, but I want it to toggle to display a + sign. <svg viewBox='0 0 26 26' focusable='true' style={toggleShow ? { tra ...

Aligning content within a div block

I created a div block that houses a form structured like this: <div class="divClass"> <form id="frm" method="post" action="/NotDefinedYet/index.xhtml" class="frmClass"> <input type="text" name="j_idt9:j_idt10:Username" placehold ...

Storing data from an API response into the localStorage using Vue.js upon clicking

My objective is to save specific data in the browser's localStorage upon clicking a link. However, the output I receive is either undefined or completely empty. <li v-for="(category, index) in categories" :key="index"> ...

Utilizing one URL to post parameters and retrieving JSON data from a different URL

Currently I am working with jQueryMobile and PhoneGap, but encountering an issue. I am trying to retrieve details in JSON format by posting parameters to one URL (URL1) and receiving the JSON response from another URL (URL2). However, I am unable to access ...

Corporate firewall causing issues with AJAX call execution

Currently, I am utilizing jQuery's $.ajax() method to retrieve approximately 26KB of JSONP data. All major browsers including FF, Chrome, IE, and Safari are successfully returning the data from various locations such as work, home, and mobile phone w ...

Refresh the dataTables after inserting new raw HTML content into a row

I need to insert a row into my table using raw HTML because I require specific [data-] values in certain cells. After this insertion, I want DataTables to update accordingly, but I'm struggling to figure out how to make that happen. I have set up a ...

Show information upon opening

I am currently working on a project that involves 4 different divs, and I need the content of each div to be displayed based on dropdown selection. I found a code snippet that was close to what I needed, but after trying to adjust it, I haven't been a ...

Encountering a TypeError while trying to run Pythonshell on my Mac device

When I run a python script in node.js using python shell, it works perfectly on my Windows system. However, I encounter an error when trying to run the same thing on my Macbook: Error: TypeError: can't multiply sequence by non-int of type 'float ...

Error: The function expressValidator is not recognized in the current environment. This issue is occurring in a project utilizing

I am currently working on building a validation form with Express and node. As a beginner in this field, I encountered an error in my console that says: ReferenceError: expressValidator is not defined index.js code var express = require('express& ...

Storing JavaScript variables in a database: A step-by-step guide

For the past few days, I've been working with CakePHP and trying to save a javascript variable into a MySQL database using AJAX (jQuery). Here's the code I've been using: <!-- document javascripts --> <script type="text/javas ...

Is there a way for my app to ask for Facebook permissions when the like button is clicked?

My website has Facebook integration, with Like buttons on the homepage that are popular and a login button that is not. I want to make the Like buttons also function as login buttons by requesting extended permissions for my app when they are clicked. I h ...

Using Backbone.sync to customize dataType

I am working on an application that utilizes a .CSV file as the primary data source for a Backbone Model. I am interested in finding the most effective approach to changing the sync method so that it uses dataType "text" instead of "json". Any insights on ...

The functionality of arguments in the whenAllDone promise/deferred javascript helper seems to fail when attempting to encapsulate existing code within a Deferred

My goal is to implement the solution provided in TypeScript from Stack Overflow: UPDATE 2 - The issue with the original answer is that it does not support a single deferred. I have made modifications to reproduce the error in his fiddle. http://jsfiddle.n ...

Pressing the Enter key will result in data fetched from JSON being logged multiple times

I am currently implementing a search feature on my React website. When a user enters a keyword in the search input, the keyword is matched in a JSON file. If a match is found, it logs "yes" to the console; otherwise, nothing is logged. Here is an example ...

Using a for loop in JavaScript to dynamically display TextBoxFor(model=> model[i].prop) in an MVC application

I need some help getting this code to function correctly: $('#ddl').change(function () { $('#cont').html(''); var count = getCount($('#ddl').val()) for (var i = 0; i < count ; ...

"Every time ajax is called, it will always generate

function lks() { var groupname = document.getElementById('groupname').value; $.ajax ({ url: 'verifyGroup.php?groupname='+groupname, type: 'get', ...

I'm having trouble anchoring my images to a specific spot on the webpage in HTML

After creating my divs in css and linking them to my index file, I encountered an issue when zooming out of the page. The images would shift to the left while the background remained centered. I needed help figuring out how to keep an image fixed on the pa ...

Pass the data received from one ajax call to another ajax call as a file

Seeking guidance on how to enhance the functionality of a form that retrieves a txt file and transferring this data to another form through AJAX. The desired workflow to accomplish this task is as follows: An AJAX call is made --> Success message received ...