On click, I want to add the value of an a-tag to an inputfield
Html:
<input type="text" title="Ort, gata eller kommun" value="something">
<开发者_JS百科;a href="#">Teramo, Italy</a>
I want it so that when the a-tag is clicked, the input-tag value is set to "Teramo, Italy"
Any suggestions?
$("a").click(function() {
$("input").val($(this).text());
});
But you would be better off assigning them classes or IDs, e.g.:
<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 targeting the input relative to the clicked anchor via traversal, e.g.:
$("a").click(function() {
$(this).prev("input").val($(this).text());
});
$('a.link').click(function() {
$('input').val($(this).text());
});
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>
<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);
});
Just an FYI, in case you're going to have more than one anchor tag and input element... You probably want to give them IDs so you can refer to them directly.
精彩评论