-3

I select all html elements with a special class using jquery:

myElements = $('.myElement');

And i iterate over the elements using the each function:

myElements.each(function () {

});

How can i now access properties or functions from the html element like the text() function for example.

Mulgard
  • 9,877
  • 34
  • 129
  • 232

1 Answers1

3

Your "special id" is called a class. . (dot) is for class and # is for id.

Example: If you have a lot of span-tags with text, you can run through all of them using .each() and then grab the text using .text()

<span class="myElements">Test 1</span>
<span class="myElements">Test 2</span>
<div class="myElements">Test 3</span>

$(".myElements").each(function () {
    console.log( $(this).text() );
});

//Console output
Test 1
Test 2
Test 3

This will log every .myElements (all the elements with class = myElements) to the console.

As you're inside a function(), you can use $(this) to target each element as you iterate through them.

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116