Remn Remn - 1 year ago 68
PHP Question

Concat 2 dynamic data using Jquery

I want to concat two dynamic data using jquery, within the input field like this

<select id="trigger" name="country" required>
<?php foreach($country as $c){ ?>
<option value="<?php echo $c->name;?>"><?php echo $c->name;?></option>
<?php } ?>

<input id="autocomplete" type="text" name="street" required>

I have try using this script


So, what I get is "Country" in the textbox of street, and i have to add the "Street" after the "Country". But if I change the "Country", the "Street" which I type earlier dissapear.

How can I change the "Country" and the "Street" without anyone vanish ?

Thanks in advance.

Answer Source

If you don't want country stacking up. You need to remove the country in the input box every time you change the select input. This is my code.

var temp = "";
  var country = $("#trigger").val();
  var street = $("#autocomplete").val();
  var put = street.replace(" " + temp, "");
  temp = country;
  $('#autocomplete').val(put + " " + country);