Utilizing a JSON value as a dynamic variable for generating search links in a Vue.js application

Help Needed

Can anyone advise on the best way to pass a value retrieved from a JSON file in a vuejs project into a link, specifically a search link?

While I am aware of various methods using other libraries or plain javascript, I'm curious if there is a recommended approach for vuejs, otherwise using plain ES6 would suffice.


Situation Overview

In my scenario, when a recipe is not found in the host database, I display a link to "find similar recipes". Since I always have a name but not a recipe, I want to dynamically generate the link text like "click link for recipes similar to Braised Duck taco".

Currently, the hardcoded url is:

https://duckduckgo.com/?q=braised+duck&t=h_&ia=recipes

However, for my link, I need to take the name of the taco and integrate it into the query.

Code Snippet - Babel

function otherTaco (str) {
  const recipeQuery 
  return {}
}

Data Field - Sample Value

"name":"Pepper Tempeh"

HTML Implementation

<a href="https://duckduckgo.com/?q=taco&t=h_&ia=web" target="_blank"> {{ taco.name }}</a>

The placeholder "taco" should be replaced with the actual value of taco.name in the link ?q=taco&t=h_&ia=web".

To see a demonstration in action, visit this CodePen link.

Answer №1

Are you attempting to achieve a similar result?

<a :href="'https://searchengine.com/?q=' + food.name + '&t=h_&ia=web'" target="_blank"> {{ food.name }}</a>

By utilizing v-bind on a tag attribute, you can then manipulate it as a JavaScript expression.

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

Cameras within the boundary of an A-frame restricted by distance

I'm trying to implement a code that restricts the camera movement in A-frame. The current functionality teleports the camera back to the position 0, 1.6, 0 when it moves 10 spaces away from the starting point on the x or y axis. However, I want to mod ...

Utilizing only JavaScript to parse JSON data

I couldn't find a similar question that was detailed enough. Currently, I have an ajax call that accesses a php page and receives the response: echo json_encode($cUrl_c->temp_results); This response looks something like this: {"key":"value", "k ...

Tips for assigning a class to a div based on the route in Angular

In my angular template, I have a basic ng-repeat with some div elements. Now, I am looking to add a class to a specific div if the $routeParams.userId matches the id of the object in the loop. You can refer to the second line of code below for clarificatio ...

Showing HTML element when the model count is zero - ASP.NET MVC View

My view has a dynamic element that switches between two options depending on the Model.Count property. Check out the code below: @if (Model.Count() == 0) { <div class="well well-lg"><h3>Everyone is present! No absences today :)</h3>& ...

Is there a way to customize the ::selection style using mui createTheme?

I'm looking to globally change the color and background color of ::selection in my app. Can anyone advise on how to achieve this using createTheme()? ...

Difficulty keeping the dropdown menu open in bootstrap 4

I am facing an issue with my dropdown menus. I want them to only toggle open or close when the dropdown menu itself is clicked, not when any of the list items or other parts of the page are clicked. Below is the code snippet that is causing the problem: ...

Conceal a row depending on a cell's value being equal to zero

Having an issue with hiding rows that have a zero value in the middle cells. The values in the middle cells are dynamically generated from a Google Spreadsheet. The code below successfully hides rows with zero values that I manually input, but it doesn&apo ...

When you delete a property from a duplicated version of an Object in JavaScript, it ends up impacting the original Object as

Let's imagine we have an array of objects, var data = [{ "sss": "sssssss", "yyy": "ssdsdsds", "www": "1212121", "Group": "Mango" }, { "sss": "sssssss", "yyy": "ssdsdsds", "www": "1212121", "Group": "Mango" }] The desi ...

What is the best way to postpone the rendering of a table?

My table is bogged down by an excessive number of rows, causing a sluggish rendering process. I'm curious about the best method to address this issue. Are there any existing libraries specifically designed for this purpose? ...

The reason why JavaScript doesn't treat "1-1" as 0 in an operation like it does with "123" to 123 during calculations

When dealing with JavaScript, the difference between "123" and "1-2" can be confusing. While "123" is recognized as a string type with a numerical value, "1-2" is also a string type but it does not allow for multiplication. Why doesn't JavaScript hand ...

Unable to save Ajax data in session array

Currently, I am developing a cart system using jquery, ajax, and php. The issue I am facing is that the text within the HTML elements is not being added to the session array. Below is the ajax code I am using: $(document).ready(function(){ $("#car ...

Using Regex and Javascript to extract the base URL from a string

I am attempting to extract the base URL from a string without using window.location. It must eliminate the trailing slash It must be done using regex instead of New URL It should handle query parameters and anchor links In essence, all of the following ...

Conceal the vacant area located at the top of the page

By clicking on the "Run code Snippet" button or visiting this link, you may notice a significant amount of empty space at the beginning of the page. The images only become visible once you start scrolling down. I am looking for a solution to hide this emp ...

What could be the reason for the sudden lack of content from the Blogger API?

For weeks, I've been using the Google API to retrieve JSON data from my Blogger account and showcase and style blog posts on my personal website. Everything was functioning flawlessly until yesterday when, out of the blue, the content section stopped ...

Report the error efficiently without terminating the process

I created a tool for users to submit data. If the submitted data does not pass validation (validation returns false), I need to respond with an error 500 status code to the user. The issue is that when I use res.status(500), it causes the program to exit ...

What is the reason that server.js is always excluded from the Webpack build process?

I am currently utilizing Vue.js 3 for the front end of my application, while employing Node/Express for the back-end. My goal is to implement server side rendering, but I have encountered some challenges along the way. As far as I can tell, the client-sid ...

What is the process for incorporating Transformer instances into the buildVideoUrl() function using cloudinary-build-url?

This particular package is quite impressive, however, it seems to lack built-in support for looping gifs. Fortunately, the provided link demonstrates how custom URL sections like "e_loop" can be created. One challenge I'm facing is figuring out how t ...

Generate a Selectpicker dynamically when a button is clicked

Wanting to insert a new row in an HTML table dynamically by clicking on an add button. The new row includes a select element with the selectpicker class. Interestingly, when I remove the selectpicker class in my script to append a new row, the select eleme ...

What is the best method for implementing ajax in Codeigniter?

Exploring the best approach to writing ajax in CodeIgniter. Is it acceptable to write ajax in views, or is it better to create a new directory named assets at the root of the CodeIgniter project folder and relocate all scripts to a js folder within our a ...

ReactJS attempting to invoke a class function using a dynamically generated button

When attempting to access the deletePost(index) method from the ShowPost class using a dynamically rendered button within the render() step in React, I encounter an issue. The button labeled "click me" successfully retrieves and prints the first item in my ...