Remn Remn - 4 months ago 19
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 } ?>
</select>

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


I have try using this script

$('#trigger').change(function(){
$('#autocomplete').val($('option:selected',this).text());
});


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

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 = "";
$('#trigger').change(function(){
  var country = $("#trigger").val();
  var street = $("#autocomplete").val();
  var put = street.replace(" " + temp, "");
  temp = country;
  $('#autocomplete').val(put + " " + country);
});