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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:46:41+00:00 2026-06-01T02:46:41+00:00

Is this possible in javascript? I’m trying to set the property name of an

  • 0

Is this possible in javascript?

I’m trying to set the property name of an object to be a “HTMLInputElement”:

var el = $('#something').get(0),
    obj = {};

obj[el] = 'some random data'; 

but I don’t think it works 🙁

I get some error:

ncaught Error: Syntax error, unrecognized expression: [object
HTMLInputElement]

But I’m not sure if it’s because of what I’m trying to do 🙂

However when I console.log(obj), it looks fine, with “object HTMLInputElement” inside key names, but I don’t know if it’s a real object, or just some string

  • 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-01T02:46:43+00:00Added an answer on June 1, 2026 at 2:46 am

    That is certainly possible. Anything between the property brackets is allowed. The toString() method is called.

    // Test case:
    var obj = {};
    obj[document.body] = '1';
    console.log(JSON.stringify(obj));  // Shows {"[object HTMLBodyElement]":"1"}
    

    Problem

    After reading your comments, I see what you’re trying to do: Creating a map of DOM elements, for later use in a jQuery wrapper. This does not work, because, as I mentioned, keys are strings.

    // Your intentions:
    var obj = {};
    var body = $('body').get(0);
    obj[body] = {secret: 'Some text'};
    $.each(obj, function(elem, data) {
        // Example:
        $(elem).text(data.secret);
    
        // Expected (filled in variables):
        $(body).text('Some text');
    
        // What actually happens
        $('[object HTMLBodyElement]').text('Some text');
        //  ^^^^^^^^^^^^^^^^^^^^^^ This is an invalid jQuery selector!
    });
    

    Solution

    Use WeakMap objects for associating objects as key-value pairs, without coercing stringa. This is not well-supported, but that can be solved easily by using a polyfill.

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

Sidebar

Related Questions

I am trying to alter svg image colors with javascript. Is this possible? Can
Is it possible to get the value from a JavaScript variable and use this
Is it it possible to do this in php? Javascript code: var a =
Just out of curiosity, anything like this possible in JavaScript ? var c, flag
I wanna implement a javascript like method in java , is this possible ?
I want to make a variable length array in Javascript. Is this possible. A
I know this is possible to be done using javascript, and I presume I
Possible Duplicate: How to parse JSON in JavaScript I have this JSON string: [{title:
We can easily alert anything in java script. Is it possible to get this
Is this case possible using JavaScript or jQuery ? When the value of the

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.