user1304948
user1304948

Reputation: 73

What does this attribute below mean?

I have seen this example on the web:

$('#questionTextArea').each( function() {

    var $this = $(this);
    var $questionText = $("<textarea class='textAreaQuestion'></textarea>")
                   .attr('name',$this.attr('name'))
                   .attr('value',$this.val());

    $question.append($questionText);

    });

Where it says '.attr('name',$this.attr('name'))', what does that mean? Does that give the same 'name' attribute as the 'id' attribute #questionTextArea or the same 'name' as the 'class' attribute 'textAreaQuestion'?

Thanks

Upvotes: 1

Views: 334

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270607

This is assigning the name attribute on each newly created <textarea> to the name attribute of #questionTextArea.

// Points $this to the current node the .each() is iterating on (#questionTextArea)
var $this = $(this);

// The name attribute of the current node .each() is iterating on
$this.attr('name');

Note that since it's an id queried, there should only be one of these, and therefore the .each() loop is kind of unnecessary. The same thing could be accomplished with something like:

var $questionText = $("<textarea class='textAreaQuestion'></textarea>")
   .attr('name', $('#questionTextArea').attr('name'))
   .attr('value', $('#questionTextArea').val());

Upvotes: 6

Related Questions