Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  145] [ 6]  / answers: 1 / hits: 15424  / 15 Years ago, fri, september 18, 2009, 12:00:00

I want to have a text field where people can type in a value. Then I want to have a href open a url with the text field appended to the end.



So if the text field says elephant then when they click the link, a page will open with at example.com/elephant.



I think javascript would be the easiest way to accomplish this but I just don't know how.


More From » jquery

 Answers
7

I'm assuming that you have some HTML like the following and want to append the contents of the appendUrl input field to the url in the anchor tag when the anchor tag is clicked.



<a href=/base/url id=baseUrl>Link Text</a>
<input type=text id=appendUrl />


Then, using jQuery, it would look something like:



$(function() {
$('#baseUrl').click( function() {
window.location = $(this).attr('href') + '/' + $('#appendUrl').val();
return false;
});
});


The basic idea is to extract the value of the input and append it to the url in the href. Use the value so constructed to set the location of the current window. I return false in the click handler to prevent the default action (the base url alone) from being taken.


[#98661] Wednesday, September 16, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zahrafrancisr

Total Points: 176
Total Questions: 105
Total Answers: 99

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
;