Results 1 to 2 of 2

Thread: Simply JavaScript - Question on hasClass

  1. #1
    Join Date
    May 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simply JavaScript - Question on hasClass

    I am trying to work some of the illustrated examples to get a better feel on how they function. Presently I am having trouble with a portion of the hasClass from page 88.

    I retyped the code to be a simple function:

    Code:
    function has(target, theClass) {
    alert(target.className);
    
    var pattern = new RegExp("(^| )" + theClass + "( |$)");
    
    if (pattern.test(target.className)) {
    return true;
    }
    
    return false;
    }
    then in a html doc I have this:

    Code:
    <body onload='has("h3", "red");'>
    
    <p class="red">I should be white background black text</p>
    <h7>no class</h7>
    <h3 class="red">Some class</h3>
    
    
    </body>
    </html>
    If I understand correctly I thought target.className would get the class name of the element passed to it, thus I expected to have the function return True, but I consistently get False. I got False whether I put in an element with the class or without it. I then put the alert(target.className) in to see what the value was showing has and I get "Undefined". So presently I am not sure if I am doing something incorrect or it maybe that I don't have a proper understanding of className. Any help would be appreciated.
    Thank you.

  2. #2
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Umm for my hasClass function i do something like this
    Code:
    return instance.classList.contains(name);
    instance = element
    name = what class you are looking for.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

Similar Threads

  1. Easy 'Simply Gallery' question
    By Sn0wman in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 04-24-2012, 01:03 PM
  2. Really simply image problem
    By MrRSMan in forum PHP
    Replies: 4
    Last Post: 10-28-2009, 11:46 PM
  3. Simply Button
    By fullereno in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 09-17-2009, 01:41 PM
  4. Simple tabs not working so simply.
    By alexjewell in forum JavaScript
    Replies: 2
    Last Post: 06-10-2007, 12:53 PM
  5. Replies: 3
    Last Post: 09-05-2005, 03:45 PM

Bookmarks

Posting Permissions

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