Apply pointer style to the CSS only when a division contains text

If there is plain text inside a div with class="x95qze", how can we add cursor:pointer only to the second div that contains the text Cursor pointer required here.

The div with class="x95qze" will not change. The content of the div may be added or removed using JavaScript.

cursor:pointer should only be applied when there is text within div class="x95qze

<!DOCTYPE html>
<html>
<head>
   <title>Cursor</title>
</head>
<body>
    <div class="x95qze">
        <div class="RiYDI">No cursor pointer required here</div>
    </div>
    
    <div class="x95qze">Cursor pointer required here.</div>
</body>
</html>

CSS:

    .x95qze {
        border:1px solid black;
        padding:5px;
    }
    
    .x95qze .RiYDI {
        border:1px solid black;
        width:50%
    }

Answer №1

Give this a shot:

HTML

<div class="y83plm"><span id="css-output">Need cursor pointer in this section.</span></div>

CSS

#css-output {
    cursor:pointer;
    display:inline-block;
}

Answer №2

if you are familiar with the text, you can utilize pure javascript includes

Start by identifying the text to search for

const sentence = 'The quick brown fox jumps over the lazy dog.';

const word = 'fox';

console.log(`The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence`);
// expected output: "The word "fox" is in the sentence"

if (sentence.includes(word)) {
   let element = document.querySelector('.x95qze');
   element.setAttribute("style", "cursor:pointer;");

}

for more information, visit MDN

Answer №3

When working with Jquery, you can utilize the text selector known as :contains(text). After updating content, a function is called that loops through elements with the class .x95qze and adds the class cursored to those without children.

In the CSS for the cursored class, a rule is set to cursor:pointer.

function checkCursors(){
  $('.x95qze:contains(Cursor)').each(function(i, container) {
      $(container).toggleClass('cursored',   $(container).children().length == 0);
   });
}
// After content updated
checkCursors();
.x95qze.cursored { cursor:pointer; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
   <title>Cursor</title>
</head>
<body>
    <div class="x95qze">
        <div class="RiYDI">No cursor pointer required here</div>
    </div>
    
    <div class="x95qze">Cursor pointer required here.</div>
</body>
</html>

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

Is there a way to retrieve user input without having to submit the form?

I am looking to capture user input without the need for submission, and then pass it through an AJAX method as a parameter to an action. Despite trying various methods, I have not been able to find a solution. Below is the code snippet: <input type="t ...

Using jQuery.post to select and manipulate the target div displaying Google search results

I am trying to implement a custom form and display the results in a specific target DIV ('results') using ajax. However, I am facing difficulties and unable to figure out what is going wrong. Even after referring to this demo (https://api.jquery ...

Extracting information from a database (HTML, JavaScript)

I am currently working on a table that receives data from the server using ajax: $.each(data, function(i, item) { $('#MyTable tbody').append("<tr>" +"<td>" +data[i].A+ "</td><td>" +d ...

Creating a visual feast: A guide to crafting a stunning image gallery

Is there a way to ensure all the pictures in a table are the same size, orientation, and inline? Currently, my images vary in size and rotation. The CSS I have applied is styling only the first image differently from the rest. Any help is appreciated! CSS ...

How to Blend Pseudo-classes in CSS?

If I want the selected text in a link to turn red when hovered over, can I achieve that using the following CSS code? .abc:hover::selection {color: red;} Also, if I have the following link: <a href="123" class="abc">4567890</a ...

Tips for modifying the color of highlighted text?

Can you help me change the color of this text from blue to dark green by using Javascript or HTML/CSS? Please left-click and drag over the text to select it. ...

Alter the appearance of the mouse cursor when hovering over input fields in HTML

I am working with a variety of HTML elements that can be moved via Drag'n'Drop. In order to indicate to the user where these elements can be dropped, I change the cursor's appearance using CSS classes applied through JavaScript. This method ...

Bootstrap4 does not support the <button> element

How can I achieve a 'collapse icon' floated to the left followed by Copyright © using Bootstrap 4? I've looked at similar questions on this topic, but none seem to provide a direct answer. Other questions contain different code with ob ...

Sending information via HTML on an iPhone

Posting form data on a website typically involves using the following HTML code with a post request: <div id="requestinfo"> <form method="post" action="http://abc/form-post.php" id="request_form"> <input type="hidden" name="fiel ...

Unexpected arrows are being added to the dropdown menus in a responsive Twitter Bootstrap menu

I am puzzled by the behavior of my responsive twitter bootstrap nav. Take a look at this screenshot: The issue is with the strange up-pointing arrows that appear. The carets are fine, but these extra arrows are not desired. They disappear if I remove all ...

Increase the dropdown size without altering the dimensions of the container

I have a dropdown menu enclosed in a div with a vibrant yellow background. Take a look at the code on Plunker here. Upon clicking the dropdown button, the list expands and the container's height increases as well. The background color of the dropdown ...

Placing text beside an image

I am brand new to osCommerce and recently took over this code. We are looking to improve the layout of the new_products page. Here is the current display code: div class="contentText"> <div class="NewProductsList"> <table border="0 ...

How to implement a scrollbar for tables using Angular

How can I implement a vertical scroll bar only for the table body in my Angular code? I want the scroll bar to exclude the header rows. Since all rows are generated by ng-repeat, I am unsure how to add overflow style specifically for the table body. Here ...

Keeping the header fixed on a sticky div (tocbot)

While working on my website, I decided to implement a Table of Contents section using tocbot. However, I encountered an issue where the Title I added above the table doesn't stay fixed when scrolling. https://i.stack.imgur.com/vCm1K.gif Here's ...

Flexbox: Distribute remaining space evenly while allowing content to wrap

Is there a method to instruct flexbox items to occupy the available space while wrapping as soon as they become smaller than their content? However, some items have a fixed width percentage. HTML <div class="grid"> <div class="grid__item"> ...

TranslateY animation glitching

I need help with expanding a box to 100% height after click, then collapsing and sticking to the bottom. I created a function in Vue framework to handle the animation, but it's not working smoothly. How can I make it less buggy? Check out the demo her ...

Several buttons converging towards a single circle

https://i.sstatic.net/NyHXu.png I attempted to create a circular layout using 6 buttons, but the end result did not form a perfect circle as intended. In the image provided, I highlighted the areas of concern in red. Below, you will find my HTML and CSS c ...

Can cookies operate on a local server?

Similar Question: Cookies on localhost with explicit domain Here is the code snippet that I am using after the user has logged in: setcookie("user",$entered_username, time()+(60*60*5)); setcookie("password",$entered_password, time()+(60*60*5)); ...

Having trouble accessing the height of a div within an Iframe

Here is the issue I am facing: I need my iFrame to adjust its size based on a div within it, but every attempt to retrieve the size of this div results in 0. var childiFrame = document.getElementById("myDiv"); console.log(childiFra ...

Tips for dynamically resizing a div element as a user scrolls, allowing it to expand and contract based on

I was working on a project and everything seemed easy until I hit a roadblock. What I am trying to achieve is expanding a div to 100% when scrolling down to the bottom of the div, then shrink it back to 90% at the bottom and do the reverse when scrolling ...