Skip to content Skip to sidebar Skip to footer

How To Replace Part Of One Text Field With A Value From Another Text Field In Jquery?

I am trying to use jQuery to update a URL (in a text field) dynamically based on the user typing a value in another field. Here's what I've got so far: http://jsbin.com/isiqus/10/e

Solution 1:

You can try:

$(document).ready(function(){
  var valueTemplate = "http://{subdomain}.mydomain.com";
  $("input#alpha").keyup(function(){
    var value = $(this).val();
    $("input#bravo").val(valueTemplate.replace("{subdomain}",value));
   });
});

It's going to keep a template string, that renders the other input field when the text is modified.

What was happening before was that you were losing your original text template in the first string replace.

Solution 2:

v.replace("subdomain",value);

statement contains error

first time when you call replace the string contains string subdomain but second time there is no strng like subdomain so it wont replace it.

refer http://jsbin.com/ojosag/edit#preview

Solution 3:

That is because you are trying to replace the string subdomain on keyup, which will get replaced on the first keyup event itself and is no longer valid for subsequent keyups

Solution 4:

By matching the pattern of the URL, you'll be good:

return v.replace(/http:\/\/(.*?)\./,'http://' + value +'.');

The pattern will match the first subdomain, so if the user adds more subdomains, the regex needs to be changed.

Solution 5:

on second keyup there is no subdomain substring in second text field

Post a Comment for "How To Replace Part Of One Text Field With A Value From Another Text Field In Jquery?"