What is the process for applying CSS to a specific element in Meteor?

I am looking to implement CSS styling for the selected item in Meteor JS. I have created a sample example with a list of names, and when a name is clicked, I want to display the details of that name. Therefore, I need to visually highlight the clicked name in the list. The names are retrieved using Publish/Subscribe methods in this example. How can I achieve highlighting the selected item in the list of names? Please refer to the code and CSS below [The CSS hover effect works, but the active state does not]. Any suggestions on what to do?

Template Code :

<template name="client">
    <div style="float:left;">
    <div style="float:left;">
   <h2> Clients List</h2>
    {{#each clientname}}      
             <div class="client {{isselected}}">{{name}}</div> 
    {{/each}}
    </div>
    <div style="float:left;padding-left:400px;">
    {{> clientmaincontent}}
    </div>
    </div>
</template>

<template name="clientdata">
<div>
<h1> This is Client Data</h1>
<div style="font-weight:bold;font-size:24px;">Name : {{cname}}</div></br>
<div style="font-weight:bold;font-size:24px;">ID : {{id}}</div> </br>
<div style="font-weight:bold;font-size:24px;">Hcare : {{hcare}}</div> 
</div>
</template>

CSS :

  .client .selected
 {
  background-color: red;
 }
.client {
    cursor: pointer;
    width:200px;
    height:20px;
    border:4px solid #eeeee8;
    border-radius: 4px;
    -moz-border-radius: 4px;
    margin: 4px;
   // float:left;
    text-align: center;
    background-color:#eeeee8;
    font-size: 20px;
}

JS Code :

Template.client.events
          ({
            'click .client' : function (e,t)
            {

              // template data, if any, is available in 'this'
              if (typeof console !== 'undefined')
              //alert("You pressed the button");
                console.log("You pressed the Client button");
                e.preventDefault();
                 Session.set("selectedClient", this.name);
                        }
              });
Template.client.isselected = function ()
             {
                console.log("Hello DD="+Session.equals("selectedClient", this.name) ? "selected" : '');
                return Session.equals("selectedClient", this.name) ? "selected" : '';
              };

Answer №1

It is recommended to avoid storing the client name in a session variable and use a more unique identifier instead. The code {{isSelected}} will add the class selected to the list of classes if the client name matches the one stored in the session variable selectedClient.

Below is an example of how it can be implemented:

<!-- This could be in another template -->
{{#each clientname}}      
  {{> client}}
{{/each}}

<template name="client">
   <div class="client {{isSelected}}">{{name}}</div> 
</template>

In the client-side JavaScript (enclosed in Meteor.isClient):

Template.client.events({
  'click': function () {
    Session.set("selectedClient", this.name);
  }
});

Template.client.isSelected = function () {
  return Session.equals("selectedClient", this.name) ? "selected" : '';
};

The following CSS can be used:

.client .selected {
  background-color: red;
}

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

Non-responsive image in Bootstrap 4

My struggle is with making my website fully responsive. Whenever I use the img tag with a width of 100% on a large screen, the image fits perfectly, but when the window is resized, a horizontal scroll appears, creating white spaces and causing the image ...

Arranging a collection of tiny divs or images side by side

Is there a way to stack multiple divs next to each other in a responsive manner, without causing any skew? I want them to line up neatly and adjust properly on different screen sizes. Should I consider using images for this or is there another method to ac ...

What is the reason for the additional space and irregular alignment of my divs when utilizing the 960 grid system?

One issue I'm facing is that elements within divs are aligning randomly without responding to any alignment tags. Additionally, some divs are creating extra space above or below their elements. I am using the 960 grid system and have not made any chan ...

The navigation bar vanishes when a picture is inserted below it

I encountered an unusual issue on my webpage. Everything was going smoothly with my navigation bar, even for mobile phones. However, as soon as I added a photo below the navigation bar, it disappeared... I'd like to keep the navigation bar visible, ...

Issue with the parent element size when containing an image smaller than 100% width

This is the default appearance with all images set to 100% width: image description here These images must maintain their aspect ratio, so I can only use percentages for sizing. When the image width is set to 60%, it looks like this: image description ...

When the size is reduced, the content spills out of the div

I made a div container and added some callouts inside it. You can find my code uploaded here https://codepen.io/muhammad-usman-the-animator/pen/mKgoNj .name { color: black; font-weight: bold; margin-left: 20px; font-size: 20px; } .det ...

Is there a way to incorporate a transition effect into the show more category button, so that hidden li items are smoothly revealed?

Essentially, I attempted to display only 10 items by default and hide the rest, which become visible when clicking on the "more category" div at the bottom. This was achieved using jQuery, and now I am looking to add some transition effects to the toggle w ...

jQuery: Revealing or concealing several divs upon selection alteration

When I populate a form, I want to display or hide multiple divs based on the OPTION selected in a DROPDOWN. Currently, my code works but the issue is that one div can be hidden or shown by multiple OPTIONS. As a result, these divs keep toggling between hi ...

Conceal the text, yet make it appear when copied and pasted without the use of javascript

Designing a short story site has its challenges. One story may require hidden text to show up when copied and pasted. Is there a way to achieve this effect using only HTML and CSS without relying on Javascript? Here is an example of the HTML code: < ...

"Implementing CSS inline styles for improved appearance on a Safari browser when visiting

Having some trouble using CSS to create rounded corners on my Facebook fan page. Everything works fine except for Safari browser. I've tried inline styles and a separate stylesheet, but no luck. Any help would be appreciated. My CSS for rounded corne ...

Creating a dynamic element in real-time with CSS

I need help with a styling issue involving multiple spans enclosed in a center tag. I want each span to enlarge when hovered over by a user, but the problem is that when I increase the size of one span, it pushes aside all the other elements and disrupts t ...

Create a captivating dropdown hover menu with animated CSS

My latest project involves creating a sleek CSS dropdown button/menu that I am now attempting to add animation to. Initially, the menu transitioned from display: none to display:block on hover, but I encountered issues animating it. So, I decided to switc ...

Is it Possible for TD Not to Function as a Hyperlink?

I've exhausted all topics but nothing seems to be working for me! My goal is to have a table completely covered with anchor tags, but for some reason, it's not displaying any links within the TD elements, not even the text! HTML <tab ...

Tips for adjusting the width of <p:panelGrid>

My <p:panelGrid> looks like this: <p:panelGrid style="width: 50px"> <p:row> <p:column colspan="2"> <p:inputTextarea/> </p:column> </p:row> </p:panelGrid> Despite trying t ...

Triggering a click event on various instances of a similar element type using the onclick function

Hey there, I'm a newcomer to Javascript. I've been practicing my Javascript skills and trying to replicate something similar to what was achieved in this example: Change Button color onClick My goal is to have this functionality work for multip ...

Develop a JSON structure that represents an array of dynamically generated fields in PHP

Need help converting data from a PHP form into a specific JSON format. Struggling to create the right array that can be easily converted into JSON format. Here is a snippet of the array structure: Array ( [user_id] => [title2] = ...

Implement a maximum height restriction on the children of the Select component in Material-UI

Currently, I am working with the Material-UI react library to create Dropdown menus by utilizing components like <FormControl>, <Select>, and <MenuItem>. The options array for these dropdown menus is quite extensive, and I am looking to s ...

Ways to determine the height of a responsive div's background image

I have a container with a background image. Inside this container, there are additional elements like my navigation bar. My goal is to make the container have the same height as the background image. I've attempted the following: .bg { ...

Switch between text box and label appearances based on focus

I am working with a template field in my gridview. This template field consists of a text box. I am looking to achieve the functionality where after the user finishes writing in the textbox and moves to the next one, the textbox that loses focus will trans ...

I am interested in showcasing a card when it is hovered over

I am having trouble with a hover effect to display a hidden card. I have two div elements, one is labeled single-album and the other hide. I used the + selector to set the hide div to display none initially, but the hover effect is not working as expecte ...