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

Struggling to figure out how to properly use clearInterval in

I've browsed through numerous resolved issues on this platform, but I'm struggling to find a solution for my JavaScript script. My goal is to send each value of an array using the setInterval function, but I can't seem to stop it once the la ...

Determine the End Date based on the Start Date

I currently have three input fields set up like this : Warranty Start Date : <input id="WarrantyStartDate" value="2015-11-22" /> Warranty period : <input id="WarrantyStartDate" value="24"/> //24 months// Warranty End Date : <input id="Warr ...

JavaScript function to substitute instead of writing

function replaceHTML(a,b) { var x = document.getElementById("canvas_html").contentDocument; x.innerHTML = b; } Although the current function works fine, I would like to update it to directly replace the existing content instead of writing new con ...

What is the best way to send messages between different sections on the same page?

Basically, in the following code: <?php $hostname = ''; $username = ''; $password = ''; $dbn = ''; try { $dbh = mysqli_connect($hostname , $username, $password ,$dbn); //echo 'Connected to database'; } ...

Error Encountered with CakePHP Model when Parsing AJAX JSON Data

I encountered a problem with AJAX while developing an application using CakePHP 2.5.3.0 My AJAX request from jQuery to CakePHP is meant to send the user's login and password, expecting a validated JSON response. However, when using a Model method wit ...

Having trouble receiving an undefined value when making an ajax call to a web service?

Whenever I make an ajax call, I keep getting an undefined value. Take a look at my code on this fiddle Link I am wondering if we can include data in json format like this: 'north': '44.1', 'south'='-9.9' , 'e ...

Something seems to be wrong with Ajax in Chrome

After successfully making an AJAX call to retrieve data from an Excel file, everything seemed to be working fine. However, when using Chrome I encountered the following error: No 'Access-Control-Allow-Origin' header is present on the requested r ...

Adjust the button's width as the text changes to create a dynamic animation

I'm trying to create an animation effect where the width of a button changes whenever the text inside it is updated. I've attempted using useRef on the button itself to grab its clientWidth and then applying that value to a CSS variable in order ...

Running a designated AJAX function from a variable by utilizing Applescript

Struggling to execute a JavaScript code within a page using Applescript. In the JavaScript, an AJAX function was defined like so: var myFunction = function () { // Here be the code... } I attempted this in Applescript: do JavaScript "document.myFunct ...

Implementing a JavaScript function that directs to the current page

I have set up my index page with a link that looks like this: <li onClick="CreateUser()"> <a href="#CreateUser">CreateUser</a> </li> When the "Create User" list item is clicked, the main page content is populated as follows: fun ...

When editing html files in Vim, the javascript indent filetype plugin is sourced unexpectedly

After experimenting with my vim settings, I noticed an unexpected behavior. When editing html files in vim, both the indent/javascript.vim and indent/html.vim filetype plugins seem to be sourced simultaneously. Is this intentional or a bug? How can I ensur ...

Toggle button color on click by using ng-click on a different button

As a newcomer to Angular, I am facing an issue. I have two buttons - button1 and button2. What I want is that when I click on button1, its color changes from green to red. Then, when I click on button2, the color of button1 should revert back to green. How ...

Having trouble with the jQuery function not working as expected? Can't seem to identify any errors in the code?

I'm attempting to capture the essence of moving clouds from this beautiful theme: (I purchased it on themeforest, but it's originally designed for tumblr) Now, I want to incorporate it into my wordpress website here: The code used to be under ...

What could be causing my input to not activate my function?

I am having issues with my input buttons not triggering my functions. Can anyone provide some insight into this problem? <div id="windowBar"> <h3>Imagine you were a superhero...</h3> <input id="WindowClose" type="button" onclick=" ...

What could be causing my jQuery switch not to function on the second page of a table in my Laravel project?

Having an issue with a button that is supposed to change the status value of a db column. It seems to only work for the first 10 rows in the table and stops functioning on the 2nd page. I'm new and could really use some help with this. Here's the ...

execute a different function once the animation has finished running with the .bind

When attempting to detect the completion of a CSS animation in order to remove a class, I encountered an issue where using .bind causes the animation end event to trigger even for the other function. You can view the complete code here. How can I ensure ...

What is the best way to scale down my entire webpage to 65%?

Everything looks great on my 1920x1080 monitor, but when I switch to a 1024x768 monitor, the content on my webpage becomes too large. I've been manually resizing with CTRL+Scroll to reduce it to 65%, which works fine. Is there a code solution using CS ...

Arrange a pair of div containers side by side on the webpage without the need to alter the existing

Is there a way to align these two div side by side? <div class="main_one"> <div class="number_one">Title A</div> </div> <div class="main_two"> <div class="number_two">Title B</div> </div> <div class=" ...

Creating dynamic web content using PHP and JavaScript

I stumbled upon a tutorial on the PHP.net website within the "PHP and HTML" manual which includes an example titled Generating JavaScript with PHP. Currently, I am experimenting with a basic demo of this concept on my own to grasp the process before attem ...

Centering an image on a webpage using CSS

Here is the code I am using for displaying my image: return <div class="imgContainer"><img src={broomAndText} alt="broomAndText" /></div> In my css file, I have included the following styling: .imgContainer { tex ...