0

I have a input button that has an href. I need to add a string after the last / in the href from an input text box. How can this get accomplish using jquery? Here is the code:

//This is the Search Button
$('#switch-fighter-search-button-link').attr("href","/fighters/search/");

//This is the Input box
var sft = $('$switch-fighter-text').val();

4 Answers 4

2
$('#switch-fighter-search-button-link').attr("href","/fighters/search/" + $('$switch-fighter-text').val());
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$('$switch-fighter-text').keyup(function(){

     $('#switch-fighter-search-button-link').get(0).href = 
                                                 "/fighters/search/"+this.value;

});

This will update on the search box change

Comments

0

Grab both values and concatenate them (separated by a ?, I'm guessing), like so:

var head = $('#switch-fighter-search-button-link').attr("href"); // get existing href
var tail = $('#switch-fighter-text').val(); // get input value
var nhref = head + '?' + tail  // join them together, separated by a ? character
$('#switch-fighter-search-button-link').attr('href', nhref); // update the button with the new href value

1 Comment

Admittedly, this could be accomplished in fewer lines (as in Dustin's example), but I figured it'd be easier to read/grok this way.
0

Append the string.

//save the base string somewhere at the beginning of your jquery
var basehref = $('#switch-fighter-search-button-link').attr("href","/fighters/search/");

//add a event handler when the text box is changed to update the button
$('#switch-fighter-text').change(function() {
    var sft = basehref + $(this).val();

    $('#switch-fighter-search-button-link').attr("href", sft);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.