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

  • SEARCH
  • Home
  • 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 7597929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:16:48+00:00 2026-05-30T22:16:48+00:00

New to jQuery, having an issue with dynamically changing the selector. <html> <head> <script

  • 0

New to jQuery, having an issue with dynamically changing the selector.

<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script>
      function addFirst(){
        var fc = document.getElementById("fc").value;
        $("#parent").append("<div id='firstChild" + fc + "'>My boring text.</div><a href='#' onClick='addSecond(\"firstChild" + fc +"\")'>Add Second Child</a>");
        fc = (fc - 1) + 2;
        document.getElementById("fc").value = fc;
    }

    function addSecond(){
      $(***THE PARENT SELECTOR***).append("<div>Some more boring text.</div>");
    }
  </script>


  <body>
    <input type="hidden" id="fc" value="1"
    <div id="parent"></div>
    <a href="#" onClick=" addFirst('#parent');">Add First Child</a>
  </body>
</html>

I need a selector that gets me the direct parent of an object when that parent object has been created dynamically, as indicated by my text “THE PARENT SELECTOR“.

EDIT

My solution to this, in case anyone else comes across this question, was to use .call inside the onClick event.

Located in the string of the addFirst() function:

<a href='#' onClick='addSecond.call(this,\"firstChild" + fc +"\"); return false;'>

Then, changed the addSecond() function:

$(this.parentNode).prepend("<div>BlahBlah");
  • 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-05-30T22:16:49+00:00Added an answer on May 30, 2026 at 10:16 pm

    It shouldn’t matter how the object was created, the selector will work the same.

    $(this).parent().append("<div>Some more boring text.</div>");
    

    As noted, this will not work unless you pass a reference to your object. You could leverage jQuery more and all of this will be much easier:

    <html>
      <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script>
          $(function() {
            $("body").on('click', '.addsecond', function() {
              $(this).parent().append("<div>Some more boring text.</div>");
            });
    
            $("body").on('click', '.addfirst', function() {
              var fc = $('.fc').length + 1;
              $("#parent").append("<div id='firstChild" + fc + "' class='fc'>My boring text.</div><a href='#' class='addsecond'>Add Second Child</a>");
            });
          });
        </script>
      </head>
    
      <body>
        <div id="parent">
          <a href="#" onClick=" class="addfirst">Add First Child</a>
        </div>
      </body>
    </html>
    

    Note, that I added a class addfirst and addsecond. These are used to add handlers to the links automatically (or more accurately, attach handlers to the body, which trigger when divs with these classes are clicked). When they are clicked, this is passed to the function, allowing you to find that object’s parent, etc.

    You could clean this code up even further, but I can’t guess how you are using it. I am guessing you want the children added somewhere different than where they are actually ending up.

    Example: http://jsfiddle.net/2LjYH/

    If I were to guess, I would make #parent wrap the “Add First Child” link, like this:

    <input type="hidden" id="fc" value="1"
    <div id="parent">
       <a href="#" onClick=" class="addfirst">Add First Child</a>
    </div>
    

    Example: http://jsfiddle.net/XjUzA/

    This seems to work more intuitively. And if you want the second child added directly after the link, you don’t want .parent().append() at all, you simply want to use .after():

    Example: http://jsfiddle.net/XjUzA/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to javascript/jquery and having a hard time with using this or $(this) to
I'm having an issue using jQuery autocomplete with dynamically created inputs (again created with
I'm very new to jQuery and I am having an issue with parsing a
I'm very new to jquery and am having a bit of an issue with
I'm having an issue with my jquery script... should an easy task, but having
I am having an issue with jquery in Chrome and Safari, the script is
I'm fairly new to jQuery and I'm having trouble with an animation that I'd
I am new to jQuery. I am having a variable increment in my code
I'm very new to JQuery, and I'm having some trouble understanding how to do
I've been trying to use the new jquery-ui, and all but the resizable function

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.