When I click, I want to add the value of the a-tag to the input field

Html:

<input type="text" title="Ort, gata eller kommun" value="something">
<a href="#">Teramo, Italy</a>

I want the value of the input tag to be set to "Teramo, Italy" when I click the a-tag

Any suggestions?

+3
source share
4 answers
$("a").click(function() {
    $("input").val($(this).text());
});

But you would be better off assigning classes or identifiers to them, for example:

<input class="location" type="text" title="Ort, gata eller kommun" value="something">
<a href="#" class="location">Teramo, Italy</a>

$("a.location").click(function() {
    $("input.location").val($(this).text());
});

Or, target the entry with respect to the anchored anchor through a bypass, for example:

$("a").click(function() {
    $(this).prev("input").val($(this).text());
});
+4
source
$('a.link').click(function() {
  $('input').val($(this).text());
});
0
source

Javascript

$('a.toinput').click(function(){
  $('#display').val( $(this).text() );
})

HTML

<input type="text" id="display" title="Ort, gata eller kommun" value="something">
<a href="#" class="toinput">Teramo, Italy</a>
0
<input id="input1" type="text" title="Ort, gata eller kommun" value="something">
<a id="a1" href="#">Teramo, Italy</a>

$("#a1").click(function() {
    $("#input1").val(this.value);
});

FYI, ... , , .

0

Source: https://habr.com/ru/post/1772956/


All Articles