Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8417191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:58:20+00:00 2026-06-10T01:58:20+00:00

I am currently learning jQuery. I know that jQuery is a custom library for

  • 0

I am currently learning jQuery. I know that jQuery is a custom library for JavaScript.

I am doing some learning examples in a book that is only using JavaScript, and to further my learning experience, I am trying to make use of jQuery for anything that might be more efficient.

So, I have this code:

function addLetter(foo) {
    $(foo).unbind('click');
    var tileLetter = $(foo).attr('class').split(' ');
    var letter = tileLetter[2].charAt(1);
    if (document.getElementById('currentWord').childNodes.length > 0) {
        $('#currentWord p').append(letter);
    } else {
        var p = document.createElement('p');    
        var txt = document.createTextNode(letter);
        p.appendChild(txt);
        $('#currentWord').append(p);        
    }
}

Question #1:

If I change document.getElementById('currentWord').childNodes.length to $('#currentWord').childNodes.length it doesn’t work. I thought the jQuery selector was the same thing as the JS document.getElementById as that it brought me back the DOM element. If that was the case, it’d make sense to be able to use the .childNodes.length functions on it; but it doesn’t work. I guess it’s not the same thing?

Question #2:

The code is textbook code. I have added all the jQuery that there is in it. My jQuery knowlede is limited, is there a more efficient way to execute the function?

The function’s purpose:

This function is supposed to create a p element and fill it with a Text Node if it’s the first time it’s run. If the p element has already been created, it simply appends characters into it.

This is a word generating game, so you click on a letter and it gets added to a ‘currentWord’ div. The tile’s letter is embedded in the 3rd css class, hence the attr splitting.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:58:22+00:00Added an answer on June 10, 2026 at 1:58 am

    Question #1:

    jQuery returns a jQuery object. To return it to a regular javascript object use $(object)[0] and you can then treat it as a plain javascript (or DOM) object.

    Question #2:

    The efficiency looks good to me. Although you might want to use spans instead of p elements.

    I guess one thing you could do (even though yours looks to run very fast) is cache the dom element:

    function addLetter(foo) {
     $(foo).unbind('click');
     var tileLetter = $(foo).attr('class').split(' ');
     var letter = tileLetter[2].charAt(1);
     var currentWord = document.getElementById('currentWord');
     if (currentWord.childNodes.length > 0) {
        $(currentWord).find('p').append(letter);
     } else {
        var p = document.createElement('p');    
        p.innerHTML = letter;
        currentWord.appendChild(p);        
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple menus that are using .slideToggle (im learning jQuery currently). Using online
I'm currently learning jQuery by reading jQuery in Action. The book discusses separation of
I am currently learning jsp/servlet, and trying to make an online bookstore. Using jquery,
I am very new and only just learning Jquery. I am currently submitting data
I am currently learning jQuery, and I am curious about something. For functions that
im currently learning python (in the very begining), so I still have some doubts
I am currently learning Javascript, and I'd like to create my own Lorem Ipsum
I am currently learning C, and am having some issues with trying to make
First, I know that there are jQuery plugins for search suggest. The reason I'm
I've just been learning some jQUery to get a basic image gallery going on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.