JavaScript jQuery - text() method? jquery text method javascript equivalent,jquery text method not working,

Being fairly new to jquery and javascript I can't seem to understand how the .text() method really works. I read through the jQuery documentation but still can't figure it out.

for (var i=0; i < arrayLength; i++){
        var currentElement = $(".artist")[i];
        var currentArtist = currentElement.text;
        console.log(currentElement);
        console.log(currentArtist);
    }

currentArtist returns "undefined" in the console. It works fine on the $(".artist") alone, but not when I use the [i] or anything additional for that matter. What am I missing here? How else could I grab a text value inside a selector?

Answer:1

By using the [] operator on jQuery object you're accessing the raw element node that was found by jQuery. This raw element doesn't have the jQuery methods anymore, nor a text property.

If you want to get single element from jQuery object and keep the jQuery wrapper, use eq method.

var artistElement = $(".artist").eq(i);
artistElement.text(); // gets the text content of the element

The code you've posted is also not very optimized. For instance, with every loop iteration you're searching the document over and over again for elements with class artist. Better to cache that search result in a variable before performing the loop. And if the loop iterates over all .artist elements, you can use jQuery's each method.

$(".artist").each(function () {
    var artist = $(this); // this poits to the raw element thus wrapping into jQuery object
    console.log(artist.text());
});
Answer:2
var currentArtist = currentElement.text;

Should be:

var currentArtist = currentElement.text();
Answer:3

You should use a each():

$(".artist").each(function(i,val){

    var currentArtist = $(val).text();
    console.log(val);
    console.log(currentArtist);
});

$(".artist") produce a jQuery object that could be like this:

[div, div, div, div, prevObject: jQuery.fn.jQuery.init, context: document, selector: ".artist"...]

So the result of $(".artist")[i] is a HTMLElement and do not have a text method, that's why you're getting undefined

Also text() is a function and may be followed with ()

But if you want to keep the for loop you can do

for (var i=0; i < arrayLength; i++){
    var currentElement = $(".artist")[i];
    var currentArtist = $(currentElement).text();
    console.log(currentElement);
    console.log(currentArtist);
}
Answer:4

When adding two or more functions in my jquery script file the function/s stops working. It's working perfectly fine when I've only written one function. But when I'm adding a second one both of ...

When adding two or more functions in my jquery script file the function/s stops working. It's working perfectly fine when I've only written one function. But when I'm adding a second one both of ...

  1. break jquery functions

Calling this async Web Method, I get a 500 error. Are there other ways to call a web method async? Stacktrace: Server Error in '/' Application. Unknown web method SendMessage. Parameter name: ...

Calling this async Web Method, I get a 500 error. Are there other ways to call a web method async? Stacktrace: Server Error in '/' Application. Unknown web method SendMessage. Parameter name: ...

  1. call method async c#
  2. call method async java
  3. call method async
  4. call async method without await
  5. call async method synchronously c#
  6. call async method without await c#
  7. call async method from main
  8. call async method from main c#
  9. call async method in constructor c#
  10. call async method in constructor
  11. call async method from sync c#
  12. call async method javascript
  13. call async method synchronously
  14. call async method from sync
  15. call async method in parallel.foreach
  16. call async method from ajax c#
  17. call async method without await javascript
  18. call async method from console app c#
  19. call async method in button click c#
  20. call async method from static main

Why I ask an easy question: Hi, lots of people asked how to split an string in javascript by multiple values, but I don't understand how it works the /[^\w\s]|_/ in the split asked, so I don't know ...

Why I ask an easy question: Hi, lots of people asked how to split an string in javascript by multiple values, but I don't understand how it works the /[^\w\s]|_/ in the split asked, so I don't know ...

  1. split string carriage return javascript
  2. split string carriage return c#
  3. string split carriage return
  4. python split string carriage return
  5. powershell split string carriage return
  6. sql split string carriage return
  7. excel split string carriage return
  8. vba split string carriage return
  9. php split string carriage return
  10. java split string carriage return
  11. oracle split string carriage return
  12. bash split string carriage return
  13. ruby split string carriage return
  14. perl split string carriage return
  15. vbscript split string carriage return
  16. excel vba split string carriage return
  17. vb.net split string carriage return
  18. c sharp split string carriage return
  19. typescript split string by carriage return
  20. jquery split string by carriage return

Used libraries: jquery-ui-1.9.1 and jquery-1.8.2 I have one widget with some function called from destroy $.widget("my.customwidget", { options: { }, _destroy: function () { ...

Used libraries: jquery-ui-1.9.1 and jquery-1.8.2 I have one widget with some function called from destroy $.widget("my.customwidget", { options: { }, _destroy: function () { ...