Learn how to customize an anchor tag in React to appear as a tile that remains highlighted when clicked

I am trying to style the anchor tag to appear as a rectangular tile (which is already in progress). When the tile/anchor tag is clicked, I need it to do the following two things:

  1. Remain selected and highlight with a green color.
  2. Upon selection or change between TILE-1, TILE-2, and TILE-3, I need to retrieve the value of the text in the input field.

Could someone provide guidelines on how to achieve this, please?

 const showTile = (): ReactElement => {
      <ul className="tileList">
        <li>
          <button href="#Tile1" class="tile" >
            TILE-1
          </button >
        </li>
        <li>
          <button  href="#Tile2" class="tile">
            TILE-2
          </button >
        </li>
        <li>
          <button  href="#Tile3" class="tile">
            TILE-3
          </button >
        </li>
      </ul>

  };
  
  const showTextBox = (): ReactElement => {
  <input type="text" value="">
  };

 const [selectedTile, setSelectedTile] = useState("");
 const [textVal, setTextVal] = useState("");
  
  return (<div> {showTile} {showTextBox} </div>);
ul,
li {
  list-style: none;
}

.tileList > li button  {
  color: grey;
  background-colour: yellow;
  border: 1px solid #ffffff;
  padding: 10px 15px;
  font-size: 13px;
}

Answer №1

Implement a dynamic class for the chosen tie and assign it to a variable. To change its value, create a 2D array and store the tie values within it. Then, develop a straightforward function to alter the tie values. This concept resembles a puzzle application; explore puzzle functions for further insight.

Answer №2

This is the method I used to solve it.

state ={
  selected: false
}

selectedHandle = () => {
 const {selected} = this.state
   this.setState({selected: true})
}


        <li onClick={() => this.selectedHandle()}>
          <a href="#Tile1"  class={{selected === true ? "active" : "passive"}} >
            TILE-1
          </a>
        </li>

If you need help accessing the value, check out this question how to use event.target.value in li

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

React Native bug: "Cannot access _this2.props.navigation.navigate as an object"

I am encountering an error in React Native and despite going through numerous posts from others facing the same issue, I am unable to find a solution. The exact cause of this problem is still unknown to me. MY APP.JS CODE import * as Expo from 'expo ...

Understanding the relationship between JavaScript UI components and their impact on HTML is essential in grasping

Many JavaScript UI components, such as jQuery UI, Bootstrap, or Kendo UI, take an HTML element and dynamically render themselves. For example: $('#someselect').autocomplete(); I am curious if these frameworks can be successfully integrated wit ...

What is the best way to target the nth-child() of a slotted element within a web component that utilizes multiple uniquely named slots?

I am struggling to select the second slotted item in a specific slot using slot[name=foo]::slotted(:nth-child(2)){, but it's not behaving as I anticipated. Even though the first foo slot is styled with green, the second one doesn't follow suit. ...

I'm trying to determine which jQuery event would be more beneficial for my needs - should I go with

I'm facing a dilemma On my website, I need to capture the value of a span within a modal. The value changes when the modal is opened and reverts to the old value when closed. This particular value represents the cart total in my online store. Wheneve ...

What causes UI components to fail to update when a user clicks buttons in React?

I have encountered an issue while working on my project. I am currently implementing a function to filter products by category and sort them in the UI when a user clicks on a category button. However, despite updating the array correctly when the button is ...

Can anyone offer any thoughts on why my specific type is not being correctly utilized in the general function?

Code Example in ReactJS: The data structure used in the render function may vary depending on the specific application. I thought using a generic would be ideal for this scenario. Can anyone explain why this approach did not work as intended? type Column ...

Guide on mapping dynamic pages in Next13.3 for efficient sitemap creation

I am currently following the recommended method for generating a sitemap in Next13.3+ by using a sitemap.js file located in the app folder. Below is an example of the code I have implemented (please note that these are just placeholder URLs and should be r ...

The background video is not working on iPhones, even though it was functioning properly in the past

After extensive searching, I have been unable to find a solution to my issue. I used an html5 video element as a background with success on both web and mobile platforms, until recently. I have tried adding all necessary attributes for compatibility and I ...

How should an admin API token be securely stored in Next.js? Storing it on the client side as an http-only cookie seems risky

Currently, I am developing an e-commerce site using next.js and the sylius API. The admin routes of the API are secured with JWT, which requires me to login to the API, obtain a token, and then use that token to fetch products. To maintain the token for ea ...

Differences in accessing the previous state between React's useCallback and useState's setState(prevState)

It has come to my attention that useCallback functions recreate themselves when their dependencies change, acting as a sort of wrapper for memoizing functions. This can be particularly useful for ensuring access to the most up-to-date state in useEffect ca ...

How to center a text box within a div using CSS

Looking for equal margins on top and bottom? Here's how to achieve it: If you have a container div like this: <div id="search"> <input id="txtSearch" type="txt"> </div> Your CSS should look something like this: #search{ m ...

What is the best way to remove headers and footers programmatically in print pages on Safari using JavaScript?

Struggling with eliminating the header and footer on print pages in Safari using JavaScript? While disabling the header and footer manually can be done through print settings in most browsers, my aim is to automate this process with code to ensure that use ...

What methods can you employ to prevent conflicts between class names?

Imagine creating a blog and assigning a class called "post" for timeline posts. Later, another team member adds a highlights section with the same "post" class, causing a collision. My usual solution is nesting classes instead of keeping anything "global, ...

Incorporate and utilize preferences

At the start of my React app, I want to fetch and load settings: fetch(`${window.location.origin}/settings`) .then(response => response.json()) .then((settings) => { ReactDOM.render(<App settings={settings} />, document.getEleme ...

Updating the CSS: Using jQuery to modify the display property to none

I am facing an issue with displaying an element that is defined as display:none in CSS. I tried to use the .show() function in jQuery, but it's not working as expected. Here's the code snippet: CSS .element { position: absolute; display: no ...

Webencore encounters an issue: Duplicate plugin/preset has been identified

Struggling to integrate React with Symfony 4, I encountered a roadblock. When attempting to build webpack, every .js file added via .addEntry() in webpack.config.js triggered the same error. Module build failed (from ./node_modules/babel-loader/lib/index. ...

Unable to adjust font for headings using CSS and Bootstrap

Looking to expand my skills in HTML and CSS, I've encountered a bit of a hiccup. Utilizing Bootstrap for my webpage, here's the simplified version of what I have so far: index.html: <!DOCTYPE html> <html> <head> <lin ...

Ways to adjust the size of Material-UI textfield without relying on react hooks

I am trying to adjust the height of a multi-line material-UI TextField within a class component to make it longer. However, the previous guides I came across on SO all involve functional components and hooks. You can view my code below or on this sandbox ...

What are the steps to make React JSX direct to "/profile" and display the profile page?

Throughout my application, I have integrated reach router to facilitate navigation between the different pages. However, I came across a navbar component that I really like and decided to add it to my app. Strangely, clicking on the "to: "/profi ...

Struggling to close the dropdown with jquery

Check out this code snippet: https://jsfiddle.net/rajat_bansal/rtapaew5/1/ The jQuery section below is causing some trouble: $(document).ready(function(e) { $(".sub-handle").click(function() { if(!$(this).hasClass('showing-sub&ap ...