yello
yello

Reputation: 301

JS - insert form value into URL

I want to create a clickable URL, it's a bit beyond my JS knowledge.

The target URL is i.e.: https://www.apple.com/at/shop/buy-iphone/iphone-se

The created at/ - works fine. I did manage to change the i.e. iPhone SE string to iphone-se but I struggle to put (append) the iphone-se string into the URL string. So both are working, but not together.

This the current status:

$('input').keyup(function(){
  $('.model').html($(this).val().toLowerCase().replace(" ","-"));
});

$('#foo-list').change(function() {
    var opt = $(this.options[this.selectedIndex]);
    var url = opt.attr('data-url');
	$('#url').html('<a href="https://www.apple.com/'+url+'shop/buy-iphone/" target="_blank"  >Apple product site</a>');

  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<select class="paselect" id="foo-list">
<option data-country="Australia" data-iso="AUD" data-cur="A$" data-url="au/"  >Australia</option>
<option data-country="Austria" data-iso="EUR" data-cur="&amp;euro;" data-url="at/" >Austria</option>
<option data-country="Brazil" data-iso="BRL" data-cur="B$" data-url="br/" >Brazil</option>
</select>

  <input name='model' type="text" value="iPhone " ><p>
  
   
  <span class="model"></span><br>
  <span id='url'></span><br>

Upvotes: 0

Views: 235

Answers (1)

matthias_h
matthias_h

Reputation: 11416

You can do it like this:

$('#foo-list').change(function() {
    var opt = $(this.options[this.selectedIndex]);
    var url = opt.attr('data-url');
    var model = $("input[name='model']").val().toLowerCase().replace(" ","-");
    $('#url').html('<a href="https://www.apple.com/'+url+'shop/buy-iphone/' + model + '" target="_blank"  >Apple product site</a>');      
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="paselect" id="foo-list">
  <option data-country="Australia" data-iso="AUD" data-cur="A$" data-url="au/">Australia</option>
  <option data-country="Austria" data-iso="EUR" data-cur="&amp;euro;" data-url="at/">Austria</option>
  <option data-country="Brazil" data-iso="BRL" data-cur="B$" data-url="br/">Brazil</option>
</select>

<input name='model' type="text" value="iPhone ">
<p>
  <span id='url'></span><br>
</p>

Upvotes: 1

Related Questions