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).
精彩评论