开发者

jQuery: How to refer to element being operated upon?

开发者 https://www.devze.com 2022-12-21 14:26 出处:网络
Let\'s say I have following jQuery line where I am trying to change the \"id\" of the element after cloning the row of a table. How can I refer to the instance of the element and pass it to \"buildCon

Let's say I have following jQuery line where I am trying to change the "id" of the element after cloning the row of a table. How can I refer to the instance of the element and pass it to "buildControlId" function.

$row.find("select [id$='ctlType']").at开发者_StackOverflowtr("id", buildControlId(???));

I could do it like this, but I am looking for shorter version without needing to declare variable.

var $el = $row.find("select [id$='ctlType']");
$el.attr("id", buildControlId($el[0]));


You can specify a function as the second parameter:

$row.find("select [id$='ctlType']").attr("id", function(){
    buildControlId($(this));
});

And your function would return the value for the attribute:

function buildControlId($param){
    return "hello";
}


$row.find("select [id$='ctlType']").each(function() {
   $(this).attr("id", buildControlId(this));
});

This way also has the advantage of working on multiple elements. Consider changing your selector to select multiple elements (if that makes sense).

0

精彩评论

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