开发者

Getting anchors title and putting into input

开发者 https://www.devze.com 2023-03-22 22:21 出处:网络
I\'m trying to understand, how I could grab anchors title atribute, and insert it into input. I tried this:

I'm trying to understand, how I could grab anchors title atribute, and insert it into input.

I tried this:

$(".choosebox a").click(function () { 
var url = $(this).attr('title');
//alert(url);
$("input").text(url);
return false;
});

Alerting works great, but the title is not inserting inside the input.

What might be the correct way to do that?

P.S. If I want point开发者_开发技巧 to input one input, not all, then which syntax is appropriate? $("input[name='this-one']") ?


The problem is, probably, using text() rather than val(), an input element doesn't really have a text() content, rather it has a value, which jQuery addresses as val(), therefore this should work:

$(".choosebox a").click(function () { 
    var url = this.title; // Using the native JavaScript/DOM method for getting the title attribute.
    //alert(url);
    $("input[name='this-one']").val(url);
    return false;
});

References:

  • text().
  • val().
  • attribute-equals selector.


use instead

$("input").val(url);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号