Add value of input field to anchor/link tag

Martijn Thomas

I'd like to append/add the value of an input field entered by the to an anchor/link tag on the same page, that links to an external website.

<input type="text" name="foo">
<a id="uniqueId" href="http://example.com/{value of foo}">Click</a>

I think I need to use jQuery for this, but have no idea on how to use it in this case. Does any one have any toughts on this matter?

btw amd for security reasons the value does also need to be url encoded

David says reinstate Monica

I'd suggest, with jQuery:

$('input[name="foo"]').on('change', function(){
        var self = this,
            $self = $(self),
            link = $self.next();
        link.prop('href', function(){
            this.href = this.protocol + '//' + this.hostname + '/' + encodeURIComponent(self.value);
        });
    });
a::after {
  content: ' (' attr(href) ')';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="foo">
<a href="http://example.com/{value of foo}">

In response to the question, asked in the comments:

One question, Can you adapt your awnser to add a unique identifier to the anchor tag <a id="uniqueId" href="">Click</a>?. Forgot to add this to the question

The answer is, of course, 'yes,' and it's rather simple (and more reliable than relying on the structure of the HTML and traversal), simply select the element based on its id:

$('input[name="foo"]').on('change', function(){
        var self = this,
            link = $('#uniqueId');
        link.prop('href', function(){
            this.href = this.protocol + '//' + this.hostname + encodeURIComponent(self.value);
        });
    });

$('input[name="foo"]').on('change', function(){
        var self = this,
            link = $('#uniqueId');
        link.prop('href', function(){
            this.href = this.protocol + '//' + this.hostname + '/' + encodeURIComponent(self.value);
        });
    });
a::after {
  content: ' (' attr(href) ')';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="foo">
<a id="uniqueId" href="http://example.com/{value of foo}">

Incidentally, with plain JavaScript this is still (of course) quite possible:

function appendValueToAnchor () {
  var link = document.getElementById('uniqueId'),
      protocol = link.protocol,
      newValue = encodeURIComponent( this.value );
  link.href = protocol + '//' + link.hostname + '/' + newValue;
}

var input = document.querySelector('input[name="foo"]');

input.addEventListener('change', appendValueToAnchor);

function appendValueToAnchor () {
  var link = document.getElementById('uniqueId'),
      protocol = link.protocol,
      newValue = encodeURIComponent( this.value );
  link.href = protocol + '//' + link.hostname + '/' + newValue;
}

var input = document.querySelector('input[name="foo"]');

input.addEventListener('change', appendValueToAnchor);
a::after {
  content: ' (' attr(href) ')';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="foo">
<a id="uniqueId" href="http://example.com/{value of foo}">

References:

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Input tag file value jquery

분류에서Dev

Jquery add input field and datepicker

분류에서Dev

find value of nearest input field

분류에서Dev

get value of input tag from td tag generated by for loop

분류에서Dev

Value of input field is being removed by directive

분류에서Dev

Get gravity forms input value for address field

분류에서Dev

Angular Set Input Cursor to next input, PS : next input tag will be dynamic add on keyEnter of previous input click

분류에서Dev

Angular Set Input Cursor to next input, PS : next input tag will be dynamic add on keyEnter of previous input click

분류에서Dev

How pass javascript function value into input tag value?

분류에서Dev

input type date, jQuery val() returns element tag and not value

분류에서Dev

add a show password button inside of input field - crispy forms

분류에서Dev

how can i add dynamic labels and text input field in xcode

분류에서Dev

add value to current value of text input in flash AS3?

분류에서Dev

Override migration default value in Rails form input field

분류에서Dev

Issues getting value from hidden input field with jQuery

분류에서Dev

Input field in <h:dataTable> always receives value of last item in list

분류에서Dev

jQuery - Appending images after input field based on value

분류에서Dev

How to push a value of an unchanged field into the target in a plugin's input parameters?

분류에서Dev

How to get tag value from input XML where tag name is stored in a variable

분류에서Dev

How to add default value to a number_field in rails?

분류에서Dev

Elegant way to show corresponding text_field_tag value in Rails 4

분류에서Dev

Insert string to <input> tag

분류에서Dev

How to Check value id from Tag Input and remove count row is 0?

분류에서Dev

How to replace input text tag into plain text of it's value using C# regex?

분류에서Dev

Ejs input_field_tag 메소드 설정 유형 속성을 5로 설정

분류에서Dev

Javascript input field not Working

분류에서Dev

SELECT field value minus previous field value

분류에서Dev

XSLT Tag with N Values to Tag with One Value

분류에서Dev

Autocomplete search box input field

Related 관련 기사

  1. 1

    Input tag file value jquery

  2. 2

    Jquery add input field and datepicker

  3. 3

    find value of nearest input field

  4. 4

    get value of input tag from td tag generated by for loop

  5. 5

    Value of input field is being removed by directive

  6. 6

    Get gravity forms input value for address field

  7. 7

    Angular Set Input Cursor to next input, PS : next input tag will be dynamic add on keyEnter of previous input click

  8. 8

    Angular Set Input Cursor to next input, PS : next input tag will be dynamic add on keyEnter of previous input click

  9. 9

    How pass javascript function value into input tag value?

  10. 10

    input type date, jQuery val() returns element tag and not value

  11. 11

    add a show password button inside of input field - crispy forms

  12. 12

    how can i add dynamic labels and text input field in xcode

  13. 13

    add value to current value of text input in flash AS3?

  14. 14

    Override migration default value in Rails form input field

  15. 15

    Issues getting value from hidden input field with jQuery

  16. 16

    Input field in <h:dataTable> always receives value of last item in list

  17. 17

    jQuery - Appending images after input field based on value

  18. 18

    How to push a value of an unchanged field into the target in a plugin's input parameters?

  19. 19

    How to get tag value from input XML where tag name is stored in a variable

  20. 20

    How to add default value to a number_field in rails?

  21. 21

    Elegant way to show corresponding text_field_tag value in Rails 4

  22. 22

    Insert string to <input> tag

  23. 23

    How to Check value id from Tag Input and remove count row is 0?

  24. 24

    How to replace input text tag into plain text of it's value using C# regex?

  25. 25

    Ejs input_field_tag 메소드 설정 유형 속성을 5로 설정

  26. 26

    Javascript input field not Working

  27. 27

    SELECT field value minus previous field value

  28. 28

    XSLT Tag with N Values to Tag with One Value

  29. 29

    Autocomplete search box input field

뜨겁다태그

보관