Skip to content Skip to sidebar Skip to footer

Attaching Textbox Value To A Link In The Form Of A Variable Without Using Submit Button

I was wondering how it would be possible to attach an HTML form's text box value to a link. For instance, I have 3 links and each link goes to a different PHP file, however, I need

Solution 1:

You'll need to dynamically change the link using JavaScript. Here's one approach:

$('a').on('click',function(e) {
  e.preventDefault();
  var url = $(this).attr('href').split('$')[0];
  window.location = url + $('#test1').val();
});

But it might be better to add an onchange event to the textbox itself, so that the HREF changes instantly and the user can see the intended destination on mouseover:

$('#test1').on('change', function () {
  var val = $(this).val();
  $('a[href*=\\?id\\=]').each(function (i, el) {
    $(el).attr('href', function (j, str) {
      return str.split('?')[0] + "?id=" + val;
    });
  });
});

Post a Comment for "Attaching Textbox Value To A Link In The Form Of A Variable Without Using Submit Button"