gibberish gibberish - 2 years ago 105
Javascript Question

jQuery underscore as first arg of function

I have the following code snippet:


<tr name="tr3" id="tr3">
<input type="text" class="common_cls" name="dt[]" id="dt1">
<input type="hidden" name="fn[]" id="fn1">
<span id="sp1"><a href="#" id="ah1">choose file</a><span>
<input type="file" name="fff[]" id="ff">
<input type="text" style="display:none">


$(document).on('click', '#add_row', function() {
var row = $("table#DocTable tr:eq(3)");
.find("a, input, span").each(function() {
.attr('id', function(_, id) {
var newcnt = id + count;
return id + count;

I wrote this over a year ago and I had assistance with the bit in question.

What is the purpose of the underscore as the first argument of the function called in the .attr method?

How exactly does that function work? Does it just replace the current
in all
tags with an incremented number?

Answer Source

The index of the current element is stored in the _ in this case. All that function does is update the attribute of each element with that element's id + count. If you instead named it index or i you may have been able to guess that it contained the index..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download