Deactivating functionality with JavaScript

I'm a bit perplexed about a section of code in my current side project. I am working on inputting data entry into my inputs and selecting options, then saving it to a variable for the image/patient utilizing the available fields (such as dropdown options and gender choices) for later use.

This system is designed to allow users to enter patient data along with corresponding images (female patient, male patient, or unknown patient), specifying the type of injury that the patient sustained, and storing the entered data in the dropdown menus accordingly.

After the user writes the necessary data, they press the "AP" button to fetch the appropriate photo based on the selected sex, which then displays the bed as occupied. This process repeats for all beds.

I also need to figure out a way to disable the data entry options shown above if an empty bed (blank image) is selected.

(Link to the jsfiddle can be found in the comments below)

Answer №1

Feel free to try out this code snippet, with added comments for clarity:

var imgProp = {
  'padding': '3px',
  'backgroundColor': '#666699',
  'borderSize': '1ps',
  'borderStyle': 'solid',
  'borderColor': '#000000'
};

// Function to check selected images and show/hide selection data
function checkSelected() {
  var allimgs = document.getElementById('multibeds').getElementsByTagName('img');
  var nrallimgs = allimgs.length;
  var selected = false
  for(i=0; i<nrallimgs; i++) {
    if (allimgs[i].getAttribute('class') == 'selected') {
      selected = true
    }
  }
  var selectiondata = document.getElementById('selectiondata');
  if (!selected) {
    selectiondata.style.display = 'none'
  } else {
    selectiondata.style.display = 'block'
  }
}

// Function to highlight image on click
function highlightImg() {
  var allimgs = document.getElementById('multibeds').getElementsByTagName('img');
  var nrallimgs = allimgs.length;
  for(i=0; i<nrallimgs; i++) {
    allimgs[i].onclick=function() {
      if(this.style.borderStyle == imgProp.borderStyle) {
        this.style.padding = 'auto';
        this.style.background = 'none';
        this.style.border = 'none';
        this.setAttribute('class', ''); // remove class for notice selected
      }
      else {
        this.style.padding = imgProp.padding;
        this.style.backgroundColor = imgProp.backgroundColor;
        this.style.borderSize = imgProp.borderSize;
        this.style.borderStyle = imgProp.borderStyle;
        this.style.borderColor = imgProp.borderColor;
        this.setAttribute('class', 'selected'); //add class for notice selected
      }
      checkSelected()
    }
  }
}
highlightImg();
checkSelected();
h1 {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: black;
}
tr, td {
  padding: 10px;
}
<body bgcolor="#E6E6FA">
    <h1 style="text-align:center;color:white">NHS Ward Bed Booking System</h1>
    <p style="text-align:center;color:black">Click on one of the beds to select and fill in the value to place a patient/edit a patient.</p>
    <div id="multibeds">
        <table align="center">
            <tr>
                <td><img id="BedLEmpty1" src="BedLEmpty.png" alt="Bed 1" width="100" height="75" border="1" onclick="" title="Patient Bed 1" /></td>
            </tr>
            <tr>
                <td><img id="BedLEmpty3" src="BedLEmpty.png" alt="Bed 3" width="100" height="75" border="1" onclick="" title="Patient Bed 3" /></td>
            </tr>
            <tr>
                <td><img id="BedLEmpty5" src="BedLEmpty.png" alt="Bed 5" width="100" height="75" border="1" onclick="" title="Patient Bed 5" /></td>
            </tr>
            <tr>
                <td><img id="BedLEmpty7" src="BedLEmpty.png" alt="Bed 7" width="100" height="75" border="1" onclick="" title="Patient Bed 7" /></td>
            </tr>
            <td>
                <td><img id="BedLEmpty2" src="BedREmpty.png" alt="Bed 1" width="100" height="75" border="1" onclick="" title="Patient Bed 1" /></td>
            </td>
            <td>
                <td><img id="BedLEmpty4" src="BedREmpty.png" alt="Bed 3" width="100" height="75" border="1" onclick="" title="Patient Bed 3" /></td>
            </td>
            <td>
                <td><img id="BedLEmpty6" src="BedREmpty.png" alt="Bed 5" width="100" height="75" border="1" onclick="" title="Patient Bed 5" /></td>
            </td>
            <td>
                <td><img id="BedLEmpty8" src="BedREmpty.png" alt="Bed 7" width="100" height="75" border="1" onclick="" title="Patient Bed 7" /></td>
            </td>
        </table>
    </div>
       <div id="selectiondata" align="center">
        <input id="btnAdmit" type="button" value="Admit patient" style="color:red" bgcolor="#00e6e6" onclick="btnAdmit_onClick()" />
        <input id="btnDischarge" type="button" value="Discharge patient" style="color:red" bgcolor="#00e6e6" onclick="btnDischarge_onClick()" />
        <select id="btnSelect">
            <option id="LocUnknown" value="Unknown">Unknown</option>
            <option id="LocHead" value="Head">Head</option>
            <option id="LocRightArm" value="Right Arm">Right Arm</option>
            <option id="LocLeftArm" value="LeftArm">Left Arm</option>
            <option id="LocChest" value="Chest">Chest</option>
            <option id="LocAbdo" value="Abdomen">Abdomen</option>
            <option id="LocLeftLeg" value="LeftLeg">Left Leg</option>
            <option id="LocRightLeg" value="RightLeg">Right Leg</option>
        </select>
        <select id="btnGender">
            <option id="GenUnknown" value="Unknown">Unknown</option>
            <option id="GenMale" value="Male">Male</option>
            <option id="GenFemale" value="Female">Female</option>
        </select>
        <br />
        <input id="txtForename" type="text" value="Forename" />
        <input id="txtLastname" type="text" value="Firstname" />
    </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

Loading a series of images in advance using jQuery

I have a series of frames in an animation, with file names like: frame-1.jpg, frame-2.jpg, and I have a total of 400 images. My goal is to preload all 400 images before the animation begins. Usually, when preloading images, I use the following method: v ...

Is there a way to display the title of a custom post type as a clickable link that directs to the corresponding post

I am trying to retrieve the title of a custom post type and link it to the related post. Here is the code I have so far: query_posts( 'post_type=custom_post_type&posts_per_page=1&order=DESC' ); while (have_posts()) : the_post(); echo " ...

Using JQuery to extract specific data from JSON strings

This JSON contains information related to shipping services { "rajaongkir": { "query": { "origin": "23", "destination": "152", "weight": 1500, "courier": "all" }, "status": { "code": 200, "description": "O ...

Difficulty encountered when attempting to utilize keyup functionality on input-groups that are added dynamically

I've exhausted all available questions on this topic and attempted every solution, but my keyup function remains unresponsive. $(document).ready(function() { $(document).on('keyup', '.pollOption', function() { var empty = ...

Mastering Fluent UI Web: Adjusting the border radius of a TextField component

I am in the process of creating a custom user input control that includes a TextField, a Dropdown, and a Button. To ensure that the TextField and Dropdown appear as a cohesive unit rather than two separate elements, I attempted to use two specific styles ...

Creating a JSON object in AngularJS is a simple and straightforward process

Is it a good practice to create a JSON object in AngularJS this way? Or is there a better solution to achieve the desired format? Edit question: I am trying to create an object in JSON format as shown below. I have written the code but facing difficulty ...

Guide on utilizing protractor to confirm equality of two spans in varying positions?

<span ng-bind="locations.selectedCount" class="ng-binding">1005</span> <span ng-bind="locations.selectedCount" class="ng-binding">1005</span> What method can I use in Protractor to verify that the values of these two spans are ide ...

Tips for steering clear of getting caught in the initial focus trap

I have implemented Focus-trap (https://www.npmjs.com/package/focus-trap) in my modal to enhance keyboard accessibility. Here is a snippet of my code: <FocusTrap focusTrapOptions={{onDeactivate: onClose}} > <div> ... </div> <Focu ...

Creating tables and defining their structure with jQuery for HTML can be done using the following steps

I have retrieved values from the database in a variable by parsing it from JSON format. Now, I need to generate an HTML table structure as shown below: <table class="table dataList fiberEngg"> <tbody> <tr> <td& ...

I am looking to have my page refresh just one time

I have created an admin page where I can delete users, but each time I delete a user, the page requires a refresh. I attempted to use header refresh, but this action caused my page to refresh multiple times. Is there a way to ensure that my page only refr ...

Identify and extract all HTML content enclosed within two specified tags, then save them in a JavaScript object

I have a similar setup in my HTML code: <div class ="personal-datas">Data1</div> <h2 class ="name">John Doe</h2> <div class ="date-of-birth">January 1st, 2000</div> <h3 class ="company">ABC Corp</h3> <h ...

Exploring dynamic loading of assemblies - comparing plugins, appDomains, StructureMap, and MEF

Currently, I am developing an application that allows users to dynamically create classes during runtime. Users are presented with a user interface where they input metadata, which is then converted into classes. Essentially, the information provided by th ...

Tips for utilizing JSON.parse

Within my ajax request, I am encountering the following code: 403: function(jqXHR) { var error = jqXHR.responseText; console.log(error); } When this error occurs, a message is displayed in the console: Htt ...

What is the goal of the output file in Webpack?

Currently, I am following the React tutorial at http://www.tutorialspoint.com/reactjs/reactjs_components.htm, and I find myself puzzled about the exact purpose of webpack entry and output filename. In the provided example, they have an index.html file and ...

Why is my YouTube webhook subscription request giving the error "Invalid hub.mode value"?

I'm attempting to sign up for a webhook (receiving notifications) on YouTube, but I keep encountering the error message Invalid value for hub.mode, despite confirming that it is set as "subscribe." Below is the content of my request: { "hub.callba ...

Place your cursor over the following element to take control

There is a paragraph that needs to be clipped when hovered over. An issue arises where the H1 text shifts its position. Only the phrase "HOVER ABOVE PARAGRAPH" should be concealed. * { margin: 0; box-sizing: border-box; } .wrapper { displ ...

What is the best way to programmatically access this WPF XAML resource?

Can you please guide me on how to programmatically access this WPF XAML resource? <Grid.Resources> <Style x:Key="lineDataPointStyle" TargetType="chartingToolkit:LineDataPoint"> <Setter Property="Background" Value="Da ...

Steps for using the ModelName.objects.filter method in HTML

I'm looking to streamline the amount of code needed in my view and aiming to achieve this directly in my HTML file: {% for committee in c %} {% for article in Article.objects.filter(committee=committee) %} <a class="post-link" hre ...

Looping over object properties using ngFor in an Angular applicationI have a question about looping

Instead of using keyValuePipe or {{data | JSON}}, I am looking for a different solution to write my data. How can I achieve this by using Object.entries, Object.keys, or Object.values? Check out the code with JSON in Stackblitz here: https://stackblitz.co ...

Are there any drawbacks to converting all instance methods into arrow functions in order to prevent binding loss?

What are the potential drawbacks of converting all instance methods into arrow functions to avoid the "lost binding" issue? For example, when using ReactJS, the statement onClick={this.foo} can lead to lost binding, as it translates to createElement({ ... ...