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

Obtain the parameter fetching identical identifier

When I click on a search result on the left, I want it to load in the right div without refreshing the page or opening a new one. The search generates three results with pagination. However, no matter which result I click, the same ID loads. Can anyone spo ...

Arranging an Array of Objects in JavaScript by dual criteria

In my JavaScript code, I have an object structured like this: myArray[0] -> 0:"62", 1:8, 2:0, 3:"11" myArray[1] -> 0:"62", 1:8, 2:0, 3:"15" myArray[2] -> 0:"48", 1:8, 2:0, 3:"04" myArray[3] -> 0:"48", 1:8, 2:0, 3:"01" myArray[4] -> 0:"62", ...

Ways to incorporate external JavaScript files into a React component

I have been attempting to incorporate external javascript files into my react component. When I included them in a basic html file using script tags, everything worked smoothly. However, I am unsure of how to achieve this within a react component. < ...

Utilizing reactjs (MERN stack) to dynamically update content on a single page based on both URL parameters and database queries

Hello everyone, please excuse my English Imagine I have page1 with content in a database, and page2 with different content in another database. Both page1 and page2 share the same template, but I want to dynamically change the content based on the URL or ...

three.js LambertMeshMaterial

Hello everyone: I've noticed that my models using MeshLambertMaterial are consuming approximately 1.6GB of memory, exceeding the capabilities of a 32-bit Chrome browser. Switching to Basic Material drops the memory consumption to around 456mb, but the ...

Updating tag content dynamically using jQuery

I have the following HTML code: <div class="selfclear"> <h3><a id="lnkName" href="/dir/subdir/?id=577">Name</a></h3> <address> Street name, 3 <br />3222 City<br />Phone. ...

What is the most effective method for locating and capturing a specific element (such as an input box) in Python Selenium using either Xpath or CSS selector

I am currently using selenium to scrape a website, primarily relying on xpath or CSS selectors to extract elements. However, I have noticed that these selectors are dynamic, which contradicts what I have read online about CSS selectors being stable. As a b ...

When using ng-click, each function is executed only a single time

After creating a function that combines two separate functions (recorder.start() and recorder.stop()), I encountered a problem where the program could only fire one of the functions at a time when they were added to one ng-click. This meant that in order f ...

What do we need to ensure that the ajax server response is executed as intended in the jQuery ajax post function?

The jQuery script provided here returns the following string: '#prayer_date'.html("03/19/1968"); However, it remains unexecuted because the function does not interact with the data variable. To make sure the above result is executed within the ...

What are some ways to utilize my mouse to launch projectiles at various locations?

Is there a way to make my projectile shoot using my mouse instead of clicking buttons? I want it to be able to shoot at any position x,y without having to click multiple buttons. # Updated projectile class if keys[pygame.K_f]: for bullet ...

updating the count of items in a react virtual list

Popular libraries such as react-virtualized, react-window, and react-virtuoso all include an item count property similar to the one shown in the code snippet below from material-ui. However, this property is typically located within the return statement. ...

Steps to generating a dynamic fabric canvas upon opening a new window

There seems to be an issue with the code below. When I click the button next to the canvas, my intention is to have a new window open displaying the canvas in full view. However, it's not working as expected. Can someone please assist me in troublesho ...

"An issue arises when using req.body and res.render as the values retrieved are

Encountering an unusual problem - when using req.body to transmit form input to another page, the data is properly displayed when a single word is used in the input field (e.g. "FullName"). However, when there is a space, such as in the example "Full Name" ...

How can we dynamically navigate to the next line within the Material UI DataGrid component when space is limited?

Currently, I am working with the datagrid component from material ui. I have retrieved some data from a database and am attempting to pass it to the datagrid component. However, I have noticed that certain fields contain long strings which do not fully app ...

Discord.JS Guild Member Cache Responses that are not recognized as valid

My automated messaging bot has been running smoothly for the past 6-8 months, but recently it encountered a strange issue with a specific user. Upon checking the cache of the Discord server it operates on, I noticed that it only returned two members - myse ...

What steps can I take to resolve the issue of the Error value not being iterable?

I encountered an issue in my table where I get the error message value is not iterable when there is no value to iterate through. How can I handle this error in my code? Snippet of Code: if (null != data && data) { data = data.map((item) => ...

The React sidebar expanded to cover the entire screen width

As I work on creating a dashboard page that will display a sidebar after the user logs in, I am facing an issue where the sidebar is taking up the full width of the page, causing my Dashboard component to drop to the bottom. You can view the image link of ...

Sliding a division using Jquery from the edges of the browser's window

<script> $(function(){ $('#right_image1').hide().delay('10000').fadeIn('5000').animate({right: '0'}, 5000); $('#left_image1').hide().delay('10000').fadeIn('5000').a ...

How to create expandable nodes with lazy-loaded children in Dynatree?

I have successfully implemented a tree navigation menu using Dynatree (). The tree consists of four levels: company, group, user, and computer. Each object within the tree is selectable, opening a page displaying the properties of that specific object. How ...

What is the method for obtaining a unique id that changes dynamically?

Having trouble accessing the dynamically generated ID in jQuery? It seems to work fine in JavaScript but not in jQuery. Here's an example of the issue: My jQuery code: var img = $("#MAP"+current_img_height); $("#map").css({'height': img.h ...