What is the process for setting an object's property as a href in an AJAX call?

I've got a quick query. How can I assign an object's property to the href attribute? I've indicated the line with a comment.

success: function (listOfTags) {
        let tagData = '';
        $.each(listOfTags, function (i, tag) {
            // HERE
            tagData += '<a href="http://localhost:5557/questions/tagged/' + tag.id + '"><li class="post-tag">' + tag.name + '</li></a>';
        });

        $('#recentTags').html(tagData);
    }

Answer №1

Check out this revised solution:

tagData += `<a href="http://localhost:5557/questions/tagged/${tag.id}"><li class="post-tag">${tag.name}</li></a>`;

To ensure proper HTML structure, the anchor tag should be inside the list item, and both should be nested within a ul element as shown below:

//before the loop
tagData += '<ul>';

tagData += `<li class="post-tag"><a href="http://localhost:5557/questions/tagged/${tag.id}">${tag.name}</a></li>`;

//after the loop
tagData += '</ul>';

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

The card header in Bootstrap card grid does not have the ability to extend all the way to the edge of

Utilizing bootstrap 4 within Laravel, I create blade.php template files. Here is a snippet of my code. The card grid layout appears satisfactory, however, there seems to be some empty space between the card titles and the edges of the cards. <div id=" ...

Blending a background image with CSS

I've set a background image for the div, which is also used for responsive design. Check out the demo My goal is to ensure that the image doesn't appear cut off in the responsive view. I'm looking for a CSS solution to blend the image wit ...

Utilize AngularJS to create a concealed input field

Currently utilizing angularjs, you can find the code at this link Desired Outcome: When the add button is clicked, I want the value of $scope.todotest to appear along with the text in the textbox. Issue Faced: Upon adding for the first time, the date d ...

The searchbar is failing to load the requested information through AJAX

I'm encountering an issue that I just can't seem to pinpoint. The goal is for the page to initially load all data, and upon hitting search, only display the requested data without refreshing the page. (Ideally, I would like to be able to modify ...

How to handle passing null values to an ASP .NET MVC controller using jQuery ajax

I've been encountering an issue where I am trying to send a stringified array from the view using $.ajax, but I keep receiving null in the controller function. Here's the code for the controller: [HttpPost] public void SaveColumnsToDb(str ...

Troubleshooting issues with WordPress email function using Ajax results in hang-ups on admin_ajax.php

I have a test page on my website that showcases a collection of images/PICLITS randomly selected from a folder 12 at a time for a slideshow. The link to the test page is here. One feature I want to implement is the ability to email the main image without ...

Issues with html5 dropdown menu not displaying properly when hovered over

As a beginner in programming, I have been tasked with creating a website using HTML5 and CSS. The main issue I am encountering is with my horizontal menu bar that has vertical sub-menus appearing on hover. The menu consists of 3 main tabs: Home, Health, a ...

Ajax is utilized to run an external page and refresh the current page simultaneously

Currently, I am retrieving data from a database and using a button to selectively delete entries. The code below is what I have been using on a button click, and it functions just as I intend: $(".delete").click( function() { if (confirm('Delete ...

What is the best way to reverse the order of echo statements in PHP, so that they

A project I was working on involved creating a blog using PHP. Here is the code I used to submit posts into the database: <form method="POST" action="makePost.php"> <input autocomplete="off" type="text" name="title"> <textarea name= ...

Working with Python and BeautifulSoup to retrieve <td> elements in a table without specified IDs or classes on HTML/CSS documents

While utilizing Selenium, Python, and Beautiful Soup to scrape a web page, I encountered the challenge of outputting table rows as comma-separated values due to the messy structure of the HTML. Despite successfully extracting two columns using their elemen ...

A more concise approach to crafting ajax requests

Lately, I've been immersed in building various web applications using php, mysql, jquery, and bootstrap. Now, I'm faced with a common issue - how to streamline my ajax queries for posting data? Writing code that is not only functional but also a ...

Optional parameters in Sammy.js

Utilizing ajax for paging has led me to choose Sammy.js, which works well. However, incorporating checkboxes to filter results poses a challenge. While defining a route for Sammy to intercept is feasible, the issue arises when I wish to avoid displaying ce ...

How to retrieve values from HTML class names using Javascript for loops but encountering issues

foreach($products as $row){ <input type="hidden" class="prodId" name="id" value="<?php echo $row['id']; ?>"> <input type="hidden" class="prodUnique" name="unique" value="<?php echo $unique; ?>"> <button id="added" ...

Ways to reduce the width of a flex box

I am facing an issue with my main container where I have two items placed on the same line. Item 1 is an image linked to a URL and it should be positioned at the far left of the container. On the other hand, item 2 contains a font awesome icon that needs t ...

What is the best way to display multiple VR scenes on a single webpage?

Currently, I am attempting to display several 360 photos utilizing the a-frame library through implementing a-scene. However, I am encountering an issue where only one scene is being rendered on my page. Are there any alternative approaches to address this ...

jquery token selection dropdown box

I am not encountering any errors with this particular issue, however upon reviewing the plugin it appears that it should only toggle "admin admin" in the dropdown list. I am currently utilizing the most recent version of jquery. Upon further investigation ...

Can anyone identify the result produced by this line of code? Utilizing the $.get method to access "http://192.168.4.1:80/" with the parameter {pin:p}

Can anyone explain the output of this line of code? $.get("http://192.168.4.1:80/", {pin:p}); I understand that it is an Ajax code that sends data through a GET request, but I am trying to manually send the same data like this ".../pin:13" or "", however ...

Utilizing JQuery within TWIG (Symfony 2): A Comprehensive Guide

I'm currently exploring the use of JQuery within TWIG on my Symfony2-built website. I have a functional table in TWIG and now I'm looking to utilize JQuery to implement column sorting functionality. <table><tr><th>cat</th> ...

What is the best way to display data retrieved through Ajax, jQuery, and JavaScript on an HTML page

I have successfully used the script below to fetch data from an api endpoint and populate my charts. Now, I want not only to display the data in my charts but also directly output it in my HTML code using something like the document.write() function. How ...

Background image color not displaying correctly

I've been attempting to overlay a transparent color on top of a background image. While the background image is displaying correctly, I'm having trouble getting the color to show up. I noticed that this question has been asked before. I tried im ...