Convert text into a clickable link

Creating a form with numerous text fields, some of which require numerical input. The main goal is to have users enter a tracking number, order number, or any other type of number that, when submitted, will open a new URL in a separate window with the specific number appended to it. In the case of an order number being entered, I'd like the form to redirect to: website.com/ordernumberfromform

However, I've encountered a challenge where I need the input to be formatted as simply website.com/ordernumberfromforminput. No id= or additional elements, just taking the value and adding it after the /. Unlike traditional validation checks, the URL will provide an error if the number is invalid. Simply inserting the text field's value at the end of the URL.

Has anyone faced a similar issue? It seems unnecessary to use JavaScript or server-side scripting for this task, making it more complicated than needed. Despite researching extensively, I am unable to find a solution. Any guidance is appreciated.

Shown below is a snippet of my code:

<div id="orderidfield">
<form action="https://www.website.com/" method="get" target="_blank">
<input type="search" name="orderNumber"/>
<!!-- Looking to have the order number posted to the URL (www.url.com/ordernumberthatisentered) without additional attributes, but struggling to achieve this due to the inclusion of an id and = sign -->
</form>
</div>

Answer №1

Here's a solution that utilizes JavaScript and HTML exclusively: Your HTML:

<div id="orderidfield">
<input type="text" name="orderNumber" id="search"/>
    <button onClick="search()">Search</button>
</div>

And here is the accompanying script

<script type='text/javascript'> 
function search()
{
    var number=document.getElementById("search").value;
    window.open("http://www.url.com/"+number);
}

</script>

Feel free to check out the demo here: http://jsfiddle.net/piyushkmr/mEfmu/2/

Answer №2

I found a solution by creating a workaround using a different post form and dynamically changing the action with JavaScript

http://jsfiddle.net/vf9rb/1/

Below is a snippet of the HTML code:

<form id="oldform" action="www.example.com">
    <input type="search" name="orderNumber"/>
</form>

<form id="trueform" action="www.example.com" method="post">
</form>

<button type="button">Click Me!</button>

Here's the JavaScript used:

$(function() {
    $("button").click(function(){
        var orderValue = $("#oldform input").val();
        var oldAction = $("#trueform").attr("action");
        var newAction = oldAction + "/" + orderValue;
        $("#trueform").attr("action", newAction);
        $("#trueform").submit();
    });
});

Answer №3

One way to achieve the desired outcome is by assigning the text field to a variable and then executing the following code:

open.window(url)

This should provide you with the expected result.

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 try/catch block proves ineffective at handling a socket connection exception

I am attempting to test connection to a non-existent socket. In this scenario, an exception is thrown and I anticipate it being caught in the try/catch block below. The function createConnection is imported from the net package. try { createConnection( ...

What is the process for advancing to the next or previous step using Angular.js?

I am currently utilizing ui-route for routing purposes, specifically for navigating through a series of sequential forms. After submitting one form, I would like to automatically move on to the next step without hard coding the step name in the $state.go( ...

Creating a Node server exclusively for handling POST requests is a straightforward process

I'm looking to set up a Node server that specifically handles POST requests. The goal is to take the data from the request body and use it to make a system call. However, my current setup only includes: var express = require('express'); var ...

Looking to extract a specific field using Angular JS?

The JSON data I need is fetched from the link provided below: http://maps.googleapis.com/maps/api/geocode/json?address=SFO When retrieving the JSON, only the parameter example ?adress=sfo should be used. This will result in fetching all values associated ...

retrieve a compilation of all versions through the JIRA REST API

Is there a way to use the JIRA REST API to gather a list of all potential values for the "Affects Version/s" and "Component/s" fields in a specific project? I'm curious if such an API endpoint exists within JIRA. ...

Ways to extract data from a JSON object

When using my web application, the Web API responds with the following JSON object: [ { "templateID":1, "template":"{\r\n \"Body\": \"sample date hete hee. Name\"\r\n}" }, { "templateI ...

The error message "Error: cannot read property ‘setDirtyAttribute’ of null" may be encountered when attempting to use the method YourModel.create({...}) in ember-typescript-cli

Encountering the error cannot read property 'setDirtyAttribute' of null even when using YourModel.create({...}) in ember-typescript-cli to instantiate an EmberObject. Model: import DS from 'ember-data'; import {computed} from "@ember/ ...

Using Ajax within a while loop

Currently, I am working on implementing push notifications for my messaging system and have encountered a strange issue. My approach involves using AJAX to retrieve recent messages. Within my PHP script, there is a while loop that processes the results, w ...

The hyperlink within the dropdown menu in HTML code is malfunctioning

I'm currently working on my personal website using HTML and CSS with textedit. I've successfully created functional links for the main navigation menu headings, but I'm encountering issues with the dropdown options. Whenever I try to click o ...

Substitute the information in the table with new data

I am working with an HTML table that has 5 columns, one of which contains dates. I need to convert the date format only if the data is not empty. To achieve this, I am utilizing moment.js for date formatting. While the date format conversion works perfect ...

Critical bug discovered in fundamental Vue.js component by Internet Explorer

My Vue.js-powered application is performing flawlessly in all web browsers, except for one... Upon attempting to launch it on Internet Explorer, a frustrating error appears: An anticipated identifier in vue.min.js, line 6 character 4872 Locating the spe ...

Exploring the world of jQuery AJAX requests by sending POST data to a PHP

I am currently in the process of building a cross-platform application utilizing HTML, CSS, and jQuery/JavaScript. My approach involves using AJAX requests to retrieve data from a database and send data to it as well. I have successfully implemented the &a ...

Instructions for displaying typed chat messages on the screen using socket.io and node.js

I am currently developing a chat application using socket.io and node.js. I have successfully connected the server and both the socket.io and client-side socket.io are also connected. However, when I type a message on the localhost page and hit enter, noth ...

Manipulate the visibility of div sections depending on the selected price and category checkboxes using JavaScript

I have a unique div setup where I'm defining data attributes to display a product list. Each div contains data attributes for category (data-category) and price (data-price). I want to be able to show or hide specific divs based on selections made usi ...

Dragging items in the horizontal list of Knockout-Sortable causes them to be pushed vertically

For my application development using knockout.js, I am implementing knockout-sortable to create drag-and-drop sortable lists. The setup involves a vertical list with each item containing a horizontal list. While the vertical lists are functioning properly, ...

Is storing HTML tags in a database considered beneficial or disadvantageous?

At times, I find myself needing to modify specific data or a portion of it that originates from the database. For instance: If there is a description (stored in the DB) like the following: HTML 4 has undergone adjustments, expansions, and enhancements b ...

Why is the button missing from the codepen?

I attempted to recreate the code from this Codepen link: https://codepen.io/jakaric/pen/mjJQvg However, unlike what you see here (in the run result), the liquid "Pretty little button" is not showing up in my local files. On Codepen, there is no library me ...

Calculator for calculating values using JavaScript data attributes

One issue is that certain elements are canceling each other out.. The value of the element is specified in the "data-price" attribute. My code is currently not valid and does not handle it properly, ideally I would like to update the total whenever a selec ...

Incorporate validation features within a jQuery method

I am struggling with some HTML and jQuery code that generates links based on user input. HTML <input type="text" id="text" placeholder="Enter text" autofocus /> <a id="link1" class="btn btn-info" href="#" target="_blank"> Search 1 </a> ...

What steps should I take to turn off caching specifically for directory listings?

Is there a way to generate a fresh file list every time without using cached data? I have a basic PHP script that fetches the contents of a directory and stores them in an array. The script works perfectly the first time it is used, but subsequent calls m ...