Creating dynamic <a> tags using JavaScript

My current view includes a div tag with 2 links - one for displaying the page in English and another for Arabic. I want to modify it so that if the page is already in English, only the Arabic <a> tag will show, and vice versa if the page is in Arabic. See the modified code below:

layout.js:-

export default function Layout({ children, lang, screenName, roomName, postTitle, screenNames, postTitles }) {  

var homeUrl = '/' + lang + "/post/" + encodeURIComponent(roomName) + "/";
  
  if (postTitle) {//Means url to be set for post screen
    var englishUrl = '/' + 'EN' + "/post/" + encodeURIComponent(roomName) + "/" + encodeURIComponent(screenNames.EN) + "/" + encodeURIComponent(postTitles.EN);
    var arabicUrl = '/' + 'AR' + "/post/" + encodeURIComponent(roomName) + "/" + encodeURIComponent(screenNames.AR) + "/" + encodeURIComponent(postTitles.AR);
  } else {//Means url to be set for screens screen
    var englishUrl = '/' + 'EN' + "/post/" + encodeURIComponent(roomName) + "/" + encodeURIComponent(screenNames.EN) + "/";
    var arabicUrl = '/' + 'AR' + "/post/" + encodeURIComponent(roomName) + "/" + encodeURIComponent(screenNames.AR) + "/";
  }
  if (lang.toUpperCase() == 'EN') {
    if (typeof window !== 'undefined') {
      document.documentElement.dir = 'ltr'
    }
  } else {
    if (typeof window !== 'undefined') {
      document.documentElement.dir = 'rtl'
    }
  }
  return (
<div className={lang.toUpperCase() == 'EN'?styles.roundedCornersBox:styles.roundedCornersBoxAR}>
        <a className={lang.toUpperCase() == 'EN'?styles.urlNoDecoration:''} href={englishUrl}><span className={lang.toUpperCase() == 'EN'?styles.engname:''}>English</span></a>
        &nbsp;<span className={styles.barcolor}>|</span>            &nbsp;
        <a className={lang.toUpperCase() == 'AR'?styles.urlNoDecoration:''} href={arabicUrl}><span className={lang.toUpperCase() == 'AR'?styles.arname:''}>العربية</span> </a>
</div>

Answer №1

The solution I came up with involved implementing a conditional statement in the div tag using React. Below is the code snippet showcasing this approach:

<div>
  {lang.toUpperCase() == 'EN' ?
    <a className={styles.urlNoDecoration} href={arabicUrl}><span className={styles.engname}>العربية</span></a>
    :
    <a className={styles.urlNoDecoration} href={englishUrl}><span className={styles.arname}>English</span> </a>}
</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

Rotating a camera in ThreeJS for a quick orbit

I am using an orbital camera that orbits around a globe with markers for users to interact with. When a user clicks on a marker, the camera moves to that specific point. To animate this movement, I am utilizing TweenMax as shown below: TweenMax.to(curre ...

Selenium and Python: Dealing with the ElementNotInteractableException when trying to input a value in a td element

My attempt to input the number 1 into the MasterPack boxes in this table is met with an error indicating that it is not interactable. https://i.sstatic.net/J79p6.png I encountered the "element not interactable" error while using the following code: driver ...

How to delete the last item of an array in AngularJS using scope

In my Angular controller, I have an array and a method for deleting objects. function($scope, $http){ $scope.arrayOfObjects = []; $scope.remove = function(obj){ var i = $scope.arrayOfObjects.indexOf(obj); if( i > -1 ){ ...

Getting the number of ticks from an rxjs Observable is a simple process that involves extracting

Is there a way to track how many times this observable has run? this.clock = Observable.interval(1000).map(function(value){ if(value == 0){ return value * 100 / 60; } return value * 100 / 60; }).take(61); I would like to kno ...

Tips for capturing a screenshot of the ESRI Map using Angular

Is there a way to capture a screenshot of the Esri map in its current state on the UI and then convert it into a PDF for download using Angular? Below is my current .ts code, but I am open to any additional suggestions. esri-map.component.html <!-- Map ...

What can I do to stop a webpage from automatically refreshing when I submit a form?

I'm currently in the process of building my very first website, and I've run into a bit of a snag that I can't seem to figure out. Whenever a user tries to add an item to the cart or increase the quantity, I want to avoid the page refreshing ...

Having trouble retrieving data from mongo db collection - data not found

For my eCommerce application, I am using MongoDB and Angular. The requirement is to retrieve items under each user in the cart. However, when trying to fetch the data using the object ID as a reference, it seems unable to find any data from the database. ...

Utilizing jQuery and AJAX, execute a PHP query based on the user's input and showcase the query's outcome without reloading the page

For the past 24 hours, I've been on a quest to find a solution. Although I've come across similar inquiries, either the responses are too complex for my specific scenario (resulting in confusion) or they are of poor quality. The crux of my issue ...

Creating a carousel of cards using JavaScript, CSS, and HTML

Here is a visual reference of what I'm attempting to achieve: https://i.stack.imgur.com/EoQYV.png I've been working on creating a carousel with cards, but I'm struggling to synchronize the button indicators with card advancement when clicke ...

Oops! The type '{}' is lacking the properties listed below

interface Human { firstName: string; lastName: string; } let human1: Human = {}; human1.firstName = "John" human1.lastName = "Doe" Upon declaring human1, an error pops up: Type '{}' is missing the following properties from type Human ...

The sequence of HTML5 DragDrop Enter and Leave events occur consecutively

I am encountering a problem with a simple drag & drop effect where the class name is changed when the drag enters or leaves, resulting in continuous entering and leaving. Take a look at this basic HTML code: <div class="box"> <div cla ...

Issue with marker functionality on Google Maps JavaScript API when conditions are not functioning correctly

I am currently working on plotting different markers on Google Maps by extracting data from a CSV file. I have incorporated the parsecsv-0.4.3-beta library to read the CSV file, and everything is functioning smoothly except for when I compare two fields to ...

Error encountered while executing npm run dev in the node module for next.js: token not recognized

Setting up a new Next.js project has been my latest task. I followed the instructions in the documentation exactly and used this command line: npx create-next-app@latest nextjs-blog --use-npm --example "https://github.com/vercel/next-learn/tree/maste ...

Updating content on a webpage via AJAX request without altering the original source code

Within the body of our document, referred to as "form.php," we have the following components: At the head section, there is a JavaScript code block: <script> function showUser(str) { if (str == "") { document.getElementById("txtHint").i ...

Verify if an Ajax request has been made

I've been trying to figure out how to determine if a request is made via AJAX in C#, but I'm having trouble getting it to work. Below is the code I'm using. I am using a method to make an AJAX call on the client side (I'm using the Acti ...

Angular HttpClient request fails to initiate

Overview: A button click on a form triggers the methodForm within the component. methodForm then calls methodService in the service layer. methodService is supposed to make an HTTP POST request. Problem: The HTTP POST request is not being made. However, me ...

Quirks in TailwindCSS template literals behavior

Looking at this specific component: const SaleBadge = ({ textContent, badgeColor }) => { return ( <Badge className={`bg-${badgeColor}-200 hover:bg-${badgeColor}-300 animate-pulse align-middle ml-2`} variant="secondary"><Pe ...

"Exploring the capabilities of Node WebKit in conjunction with Serial

Currently, I am in the process of building an application using Node WebKit and require access to the SerialPort on my Windows 8 PC. To install third-party modules with C/C++ addons, I followed the instructions outlined in this guide: https://github.com/n ...

The call signatures for `node-fetch -- typeof import("[...]/node-fetch/index")'` are not properly defined

Originated from this source: https://github.com/node-fetch/node-fetch#json ... my personal code: const fetch = require('node-fetch'); async function doFetch() { const response = await fetch('https://api.github.com/users/github'); ...

The variable referencing an unidentified function has not been defined

I've created an anonymous function assigned to a variable in order to reduce the use of global variables. This function contains nested functions for preloading and resizing images, as well as navigation (next and previous). However, when I try to run ...