Is there a way for me to incorporate a feature that lets the user specify the URL based on their input in a search box?

Currently, I am working on a project that involves creating two text fields for user input, along with a "View Report" button.

My goal is to have the button redirect users to a specific URL structured like this: http://example.com/reports/example/reports/TEXTFIELD1/reports/example/TEXTFIELD2/reports.aspx

This way, the URL will remain consistent except for the parts that the user enters to customize it.

In the past, I used an onclick JavaScript function to direct users to http://example.com/TEXTFIELD1. However, since my current task involves modifying multiple sections of a longer URL, I'm uncertain if the same approach is applicable.

I would greatly appreciate any assistance. If my explanation is unclear, please let me know so I can provide more context.

Answer №1

When creating links, you have the option of utilizing Template literals or simply using the "+" operator.

document.location.href = `http://example.com/reports/example/reports/${TEXTFIELD1}/reports/example/${TEXTFIELD2}/reports.aspx`

Alternatively,

document.location.href = "http://example.com/reports/example/reports/" + TEXTFIELD1 + "/reports/example/" + TEXTFIELD2 + "/reports.aspx"

In both cases, TEXTFIELD1 and TEXTFIELD2 represent values from your text fields.

Answer №2

Upon inspecting your pen, I noticed some issues that may have led to the failure of your code.

Allow me to clarify why.

var fieldOne = document.getElementById('field-one').innerHTML
var fieldTwo = document.getElementById('field-Two').innerHTML

let button = document.getElementById('button');

button.addEventListener('click', () => {
   window.location.href = 'www.website.com/reports/' + fieldOne + '/reports/' + fieldTwo + '/reports.aspx'
});

Firstly, it is incorrect to use ‘ quotation marks in JS. You should use ' " ` instead.

The second issue lies in how you are retrieving the field values incorrectly in two different ways. To fetch the value of an input field, you must utilize .value instead of .innerHTML.

Furthermore, you are fetching the input field values when the document is ready, resulting in them always being empty.

Moreover, you referenced field-Two, whereas it should be field-two (JS is case-sensitive)

Besides these errors, your code should function correctly. Let me present a revised version:

let button = document.getElementById('button');

button.addEventListener('click', () => {
  var fieldOne = document.getElementById('field-one').value
  var fieldTwo = document.getElementById('field-two').value

  var url = `www.website.com/reports/${fieldOne}/reports/${fieldTwo}/reports.aspx`;
  // or
  var url = 'www.website.com/reports/'+ fieldOne +'/reports/'+ fieldTwo +'/reports.aspx';
  window.location.href = url;
});

Notice how I store the URL in a variable for clarity. While not necessary, I find it helpful. You can choose between the two methods of declaration based on your preference (note the different types of quotation marks depending on whether you use template literals or not)

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

Discovering an HTML Element in a JavaScript Array with Specific Styling: A Step-by-Step Guide

I am in the process of developing a website that consists of different sections. The concept is simple - by clicking on a button located at the bottom of the page, it will reveal the corresponding div section. For styling purposes, I have initially hidden ...

When utilizing the map function with an array containing 168 objects in the state of a React application, a freeze of around 1

I encountered an issue when trying to update a property in a state array of objects after executing an axios.put request. Here is my code: States - useEffect //... const [volunteers, setVolunteers] = useState([]); //... useEffect(() => { async fu ...

Youtube does not trigger injection scripts in Safari extensions

During my work on a Safari extension, I encountered an issue with adding an End Script to Youtube pages. The script functions properly when the initial Youtube page loads, but if I click any links within the page, nothing happens unless I open them in a ne ...

"Utilize jQuery to target and highlight specific HTML elements when redirecting

I am working with a list of elements and I want to be able to redirect the focus to a specific element using a URL. <div id="20"></div> <div id="21"></div> <div id="22"></div> // example url http://localhost: ...

Local storage has the limitation of being able to store only a single object at a time, rather

This code is designed to store product details in a local storage variable whenever a user visits a product page, with the intention of remembering the last visited products. However, the variable currently only retains one product at a time instead of add ...

Fixed sidebar layout in Bootstrap 5 - Content getting pushed down by the table

In my Bootstrap 5 layout, I have a fixed-width left sidebar and a responsive table on the right. The sidebar is set to be hidden at smaller screen sizes using d-xl-block. .body { width: auto; } .side { background:gr ...

Using HTML5 input type number along with a regular expression pattern

In order to display only numbers and forward slashes in the input field with regex pattern, I attempted the following code: <input type="number" pattern="[0-9\/]*"> However, this code only shows the number pad from 0 to 9. How can I modify the ...

"Implementing a function to automatically input a selected value from a dropdown menu into a text field

I'm facing an issue and need some help. I have a form with one select field and one text field. What I want is for the text field to display a value corresponding to the option selected in the select field. For example, if I choose "B" from the select ...

Establishing updated file path for resources in JavaScript based on environment

I'm currently facing an issue with my external Javascript file that uses the getScript() function to execute another JS file. Both files are located on static.mydomain.com, as I am trying to set up a Content Delivery Network (CDN) for the first time. ...

Tips for displaying an element in a fresh container using jQuery

I am looking to add draggable functionality to the objects in columns on my page. Below is the script I have implemented: <script> $(document).ready(function () { $('.application').draggable({ helper: 'clone' }); ...

Concealing table columns using JavaScript - adapt layout on the fly

I am currently implementing a responsive design feature on my website, where I need to hide certain columns of a table when the viewport size decreases beyond a specific point. Initially, I tried using .style.visibility = "collapse" for all <tr> elem ...

Posting values using AJAX in PHP - A guide

test.html <html> <!-- To access the complete PHP-AJAX tutorial, please visit http://www.php-learn-it.com/tutorials/starting_with_php_and_ajax.html If you found this tutorial helpful, a backlink to it would be greatly appreciated. ...

Find a particular image across various pages using Jquery and AJAX

Is it possible to search through multiple pages on a website to locate a specific image using a Jquery/AJAX approach? My website is regularly updated, causing content to shift across pages. I need to manage links to individual images (using Jquery to assi ...

Tips for adding a checkbox to a form using Handlebars and SQL

I'm working on a form that allows users to edit a product: https://i.sstatic.net/4PJis.png After making edits, the updated information is uploaded by modifying the SQL table named "product". https://i.sstatic.net/erdiX.png However, I've encoun ...

Organize all ngDialog instances within a factory and access them through a directive

In order to keep the ngDialogs centralized in one place instead of dispersed, I had the idea of creating a factory named modal.js. This factory would contain a list of all the modals with a switch statement. Here is an example: .factory(&ap ...

Getting a Node module from the renderer in Electron: What you need to know

Is there a way to import a node module in my Electron app's renderer.js file? I'm attempting to utilize the Store object from the sindresorhus/electron-store package within my renderer.js file. This file is called through index.html as shown bel ...

Combining ApolloProvider and StatsigProvider in ReactJs: A Step-by-Step Guide

Currently in my React (Next.js) application, I am utilizing statsig-react to switch between mastergraphql endpoint URLs. However, I am encountering an issue when trying to connect statsig with Apollo. This is how my app.js file looks like: const apollo = ...

Left-align the tab elements

I am looking to customize this example for Vertical tabs. Sandbox: https://codesandbox.io/s/v0p58 My goal is to have the tabs aligned on the left side. I attempted to include: alignItems: 'left' tabs: { borderRight: `1px solid ${theme.palet ...

JQuery causing array to not update properly

I am attempting to splice an array to remove an object from it. My approach involves using angular-formly for form display, and AngularJs with JQuery to manage the data. Utilizing JQuery $(document).on("click", ".delete-me", function () { var id = $( ...

Is it possible to personalize CSS properties using PHP sessions?

Currently, I am a beginner in PHP and enrolled in a technical school class to learn more about it. Our latest assignment involves using sessions to modify four CSS properties: body background-color, a:link color, a:hover color, and h1 color. The task inclu ...