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 7948263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:40:29+00:00 2026-06-04T01:40:29+00:00

I was wondering if it were possible to, instead of selecting an element from

  • 0

I was wondering if it were possible to, instead of selecting an element from the DOM via a jQuery selector, if I can create one instead.

For example:

$.create('#hello').insertAfter('#test');

would generate the following:

<div id="test">This element already existed in the DOM.</div>
<div id="hello"></div>

Or, better yet, for more complex operations:

$.create('#test.a.b');

for:

<div id="test" class="a b"></div>

Obviously, more complex operations like :selected or :nth-child or :not() and all of those need not be implemented.

Is anyone aware of a solution?

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-04T01:40:30+00:00Added an answer on June 4, 2026 at 1:40 am

    To create <div id="hello"></div>

    try

    $('<div id="hello"></div>').appendTo('body');
    

    To create <div id="test" class="a b"></div> same as

    $('<div id="test" class="a b"></div>').appendTo('body');
    

    $('<div id="test" class="a b"></div>') will create new element but not add it to DOM.

    To add that element you need to use append() or appendTo() or insetAfter() or insertBefore() and so on.

    It is better to use a function to:

    function createElement(el, target) {
       $(el).appendTo(target);
    }
    

    use the function like:

    createElement('<div id="test" class="a b"></div>', 'body');
    

    You can also append newly created element to any other target instead of body.

    I think following function will help you:

    function createElement(el, prop, target) {
      var props = prop.split('.'),
          newelement = $(el),
          id = '',
          className = '';
        $.each(props, function(i, val) {
            if(val.indexOf('#') >= 0) {
               id += val.replace(/^#/, '');
            } else {
               className += val + ' ';
            }
        });
        if(id.length) newelement.attr('id', id);
        if(className.length) newelement.attr('class', className.trim());
    
        $(newelement).html('Newly born').appendTo(target);
    }
    
    createElement('div', '#test.a.b', '#con');​ // will work for #test.a.b or, .a.b or, #test.a
    

    DEMO

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

Sidebar

Related Questions

Just wondering is it possible to build CLI app that can be run from
I'm wondering if it's possible to use switch statement instead of if-else one in
I am wondering if it is possible to write count(vanilla) instead of vanilla.count() ?
I was wondering if it is possible to build a cxf-bc with WS-SecurityPolicy instead
I was wondering if it was possible to return a div, instead of a
Possible Duplicate: launch facebook app from other app I was wondering if there is
I am calling python functions from C++. I was wondering if it is possible
Hi I was wondering if it would be possible with the GWTP, create Presenters
I am wondering whether it is possible to assign second background-image to element through
I'm wondering how it's possible that jQuery objects show up as an array in

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.