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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:49:28+00:00 2026-05-28T02:49:28+00:00

Please have a look at the following code: <HTML> <HEAD> <script src=//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js type=text/javascript></script> <SCRIPT

  • 0

Please have a look at the following code:

<HTML>
    <HEAD>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

        <SCRIPT type="text/javascript">
            function test(target) {     alert(target.nodeName); }
        </SCRIPT>
    </HEAD>

    <BODY>
        <DIV>
            <ul>
                <li onclick="test(this)">This is fair</li>
                <li onclick="test(this)">No its not</li>
                <li onclick="test(this)">Why not</li>
                <li onclick="test(this)">Becoz...</li>
            </ul>
        </DIV>
    </BODY>
</HTML>

The function test receives target (li node) as an argument.

Now, can I somehow convert this variable to jQuery $(this) or $(e.target) or any other jQuery variable to so that I could traverse the document using the jQuery way?

  • 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-28T02:49:28+00:00Added an answer on May 28, 2026 at 2:49 am

    Converting DOM element to jQuery object

    To convert DOM element to jQuery object you do the following:

    var jquery_object = jQuery(dom_element);
    

    So, in your example it will be $(this) or $(event.target) – depending on whether you want current element or the element that actually fired the event (in your case they are the same, unless event will be fired by some descendant element).

    Converting jQuery object to DOM element

    To convert jQuery object to DOM element, you can simply treat jQuery object as array or use its get() method like that:

    var dom_element = jquery_object[0];
    

    or

    var dom_element = jquery_object.get(0);
    

    The above will return first object stored in the jQuery object – if there is only one, there should be no problems (if there are more, just change 0 into other index to get appropriate element, or just omit the argument for get() to retrieve all the elements as array).

    Your code changed to use jQuery

    Your code could look like this (if you insist on using hard-to-maintain event attributes):

    <HTML>
    <HEAD>
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    
    <SCRIPT type="text/javascript">
        function test(target) {     alert(target.get(0).nodeName); }
    </SCRIPT>
    
    </HEAD>
    <BODY>
    
    <DIV>
        <ul>
            <li onclick="test($(this))">This is fair</li>
            <li onclick="test($(this))">No its not</li>
            <li onclick="test($(this))">Why not</li>
            <li onclick="test($(this))">Becoz...</li>
        </ul> </DIV>
    
    </BODY>
    

    except in this case using jQuery is completely useless and you can just operate directly on DOM elements, converting them to jQuery when needed 🙂

    Your solution changed to bind events outside <body>

    Your code using jQuery for binding events in jQuery 1.7+ could look like this:

    <HTML>
    <HEAD>
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    
    <SCRIPT type="text/javascript">
        $(function(){
            $('li').on('click', function(event){
                alert(event.target.nodeName);
            });
        });
    </SCRIPT>
    
    </HEAD>
    <BODY>
    
    <DIV>
        <ul>
            <li>This is fair</li>
            <li>No its not</li>
            <li>Why not</li>
            <li>Becoz...</li>
        </ul> </DIV>
    
    </BODY>
    

    See the above in action here: jsfiddle.net/tadeck/2PqPP/

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

Sidebar

Related Questions

am new here. i have a slight problem; PLease look at the following code
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Please have a look at the following code, which I have run in VB6
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
please look the following code: <?php if ($_GET['picture'] == 1) { echo <img src=\1.jpg\
Please have look on the following code: $_SESSION[process_y] = new Process(); $process_y = $_SESSION[process_y];
Please have a look a the following code sample, executed on a Windows-32 system
please have a look at the following code: (I removed all doctypes etc for
please have a look at the following code. When the value of i ==
Please have a look on the following code - var abc_text = Hello; var

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.