www.webdeveloper.com
Results 1 to 5 of 5

Thread: Getting an array of class names starting with string

  1. #1
    Join Date
    May 2010
    Posts
    168

    Getting an array of class names starting with string

    Okay, I have code, it works. This is more of a learning jQuery thing for me because I couldn't find a shorter way of doing it. I'm still learning who's responsibility it is for what.

    Is there a quick jQuery one liner to return (as an array) all class names that start with "{insert_string_here}"

    The equivalent of the following:

    Code:
      var response = $( "*[class^='arete']");
      var myClassArray = [];
      for( var ix=0; ix < response.length; ix++)
      {
         myClassArray[ ix] = $( response[ ix]).attr( 'class');
      }

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Hi,

    Have you tried using
    .get()?

    Code:
    var response = $("*[class^='arete']").get();
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  3. #3
    Join Date
    May 2010
    Posts
    168
    You're going to have to explain that one to me, from what I read, get is just a short hand for an ajax call. I'm looking specifically for the class names.

    http://api.jquery.com/jQuery.get/

  4. #4
    Join Date
    May 2010
    Posts
    168
    ah, I looked at the wrong one...stupid google...

    Just clicked your link, that still gives me the elements rather than the values of class. I use the non-jQuery return value instead of get(), same difference on that part though.

  5. #5
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Quote Originally Posted by janusmccarthy View Post
    ah, I looked at the wrong one...stupid google...

    Just clicked your link, that still gives me the elements rather than the values of class. I use the non-jQuery return value instead of get(), same difference on that part though.
    There's not really a "one-liner." Although the following code could fit into one line, it's still an iterative command (loop).

    Code:
    var response = [];
    
    $("*[class^='arete']").each(function (){
     response.push(this.className);
    });
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles