Formatting specific elements within an array

I am working with an array of names, some of which are repeated. These names are then split in half and displayed as li. One thing I am struggling to figure out is how to style the name Joeyc with text-decoration: line-through; on all instances of .book where joeyc is present. Check out my code snippet below along with a link to a JSFiddle:

 <div id="book1" class="book">
       <ul class="hardcover_front">
        <li></li>
        <li></li>
      </ul>
       <ul class="hardcover_back">
        <li></li>
        <li></li>
      </ul>
       <ul class="book_spine">
      </ul>
     </div>
    .
    .(similar blocks for book2-book10)
    .

<script>
var votenames = ["Joeyc", "JakeP97", "Joeyc", "TheKid", "Joeyc", "TheKid", "Joeyc", "JakeP97", "ExploreMeDora", "Alvaro"];
var ballots = ["#book1", "#book2", "#book3", "#book4", "#book5", "#book6", "#book7", "#book8", "#book9", "#book10"];

function splitName(plName,ballotNum) {
    var halfplName = Math.round(plName.length / 2);
    var firstplName = plName.substr(0, halfplName);
    var lastplName = plName.substr(halfplName, plName.length);
    $(ballotNum + ' ul.hardcover_front').find('li:nth-child(2)').html(firstplName);
    $(ballotNum + ' ul.hardcover_back').find('li:nth-child(1)').html(lastplName);
}

for (i=0; i<ballots.length; i++) {
    splitName(votenames[i],ballots[i]);
}
</script> 

Here's the link to the JSFiddle.

Answer №1

While CSS selectors are unable to target elements based on their content, jQuery selectors offer a solution for this limitation:

$('li:contains("two")').css("text-decoration", "line-through");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<li>one</li>
<li>two</li>

However, an alternative approach might be to dynamically generate the HTML code using your script. This way, you can easily apply CSS styles or classes to specific elements based on the corresponding array item.

Answer №2

If you want to find a specific <li> element based on a certain condition, you can use an if statement to search for it.

Once you have located the desired element, you can apply a CSS class using the following code snippet:

document.querySelector("#myelement").classList.add("myclass");

This will allow you to add your preferred style to the element.

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

Assigning a value to a cell depending on a specific condition

Currently, I have a column connected to a field known as state. The possible values for this field are either S or L. My objective is to map these values as follows: S => Short, L => Long The binding is structured in the following way: $scope.grid ...

Having Trouble Scooting Down the Page on Mobile?

Hello there! I'm having a bit of trouble trying to figure out why the scrolling functionality isn't working on my small web app when it reaches the media breakpoint for mobile devices. If you'd like to take a look, here's the link to ...

Having trouble generating an Xpath that can partially match a CSS selector like `a[href*="https://stackexchange.com/"]`

Looking to extract the Href of Hot Network Questions from Is there an Xpath equivalent to a[href*="https://stackexchange.com/questions?tab=hot"] //a[Href*"https://stackexchange.com/questions?tab=hot"] The code above works well, but... //a[@href='h ...

Encountered an error: Unable to access property '0' of an undefined variable

Below is the script I am using: <script> var count; var obj; function search() { xhr = new XMLHttpRequest(); xhr.open("GET", "test.json", true); xhr.send(null); xhr.onreadystatechange = function() { if (xhr.readyState == 4 & ...

What is the process by which Oracle HRMS retrieves the complete description of a job posting?

With my limited understanding of JavaScript, I noticed that the HRMS refreshes the page and displays the job details under the same URL as the job list page. I expected to find job detail information in the network requests, but upon inspecting them, I dis ...

Matching the scope property in AngularJS directives

I am currently working on creating a custom directive that will perform regex validation on specific input fields. The goal is for the directive to determine which regex pattern to use based on an attribute provided in the input element. Here is an exampl ...

How can CSS grid be used to move a particular <td> element?

Currently, I am working on creating a dynamic table using CSS grid and I am looking to slightly adjust the left-most <th> and <td>. I am uncertain about how to move a specific element within a <table>. My goal is to achieve a layout simi ...

Exploring Angular14: A guide to efficiently looping through the controls of strictly typed FormGroups

Currently, I am working on upgrading my formGroups to be strictly typed in Angular v14. Within my FormGroup, there is a specific block of logic that iterates through all the controls and performs an action (this part is not crucial as I am facing issues be ...

Executing PHP code on button click in HTMLThe process of running a PHP script when

I am currently working on a project that involves detecting facial expressions using Python. However, I need to pass an image to this code through PHP. The PHP code provided below saves the image in a directory. How can I trigger this code using an HTML ...

Error encountered in ASP.NET Core MVC due to dependency inversion with CRUD operations

Today I delved into dependency inversion. Prior to implementing this concept, my CRUD system was functioning well, but now I am encountering some errors. The error message says "Cannot implicitly convert type int to ContractLayer.UsersDto." In the image pr ...

Unable to include a JavaScript file within another JavaScript file

Currently, I am working on a project where I am utilizing Django for the backend and HTML/CSS/JS for the frontend development. On a specific HTML page, I am including two JS files: dom_creator.js and console_page.js. My goal is to access the functionaliti ...

The HTML file generated from a Quarto document (.qmd) seems to be experiencing compatibility issues for users other than myself

As a beginner in R, I have encountered an issue when trying to share my R Quarto document with others who are not familiar with R. I prefer to share it as an html file, but the appearance of the document changes significantly when opened on another person& ...

Wait for the reaction from react router history to go back

After clicking the submit button in a form, I need to navigate backwards using history.goBack. However, if there is no previous page in the history, I want to redirect to a screen displaying a thank you message using history.replace. const handleSubmit = ( ...

I'm looking for a Visual Studio add-in that can identify and flag any unused CSS classes or IDs within the entire solution

Exactly as the headline suggests. If not, what are some reliable online services you've tried for decluttering oversized stylesheets? ...

Populate a form with database information to pre-fill the fields

So I have this web form built with HTML, and there are certain values within the form that can be changed by the user. To ensure these changes are saved, I'm storing them in a database. My goal is to have the form automatically filled with the data fr ...

How can I acquire a duplicate of a Webgl texture?

I have a webgl texture and I have stored it in a JavaScript variable var texture1 = CreateTexture() function CreateTexture(){ var texture = gl.createTexture() // more WebGL texture creation code here return texture } I am looking to create a copy o ...

What is the best way to change the background color for my photo gallery?

I'm currently working on a project to create a unique photo gallery where each image has a different colored background. I have six images in total, but right now all of them have a pink background. I've attempted adding another .popup class in t ...

Problem with datepicker interface not aligning properly with Bootstrap grid

I am encountering a challenge with aligning the input controls in my UI design. Specifically, I am trying to create rectangular borders/containers around each set of input controls and looking for a way to achieve this using Booststrap. For reference, y ...

Can anyone help me with displaying auto complete HTML files in VScode?

As a Korean, please excuse my lack of proficiency in English. This image displays the autocomplete feature for HTML files in IntelliJ. This particular file was generated by Spring Boot (start.spring.io). How can I achieve the same in VSCode? ...

Ways to merge two select options in a form

I'm attempting to merge the selections from two dropdown menus in a form into one variable before submitting the form. Here is an overview of my code: In new.html.erb (for RoR): <%= form_for :character, url: characters_path, method: :post do |f| ...