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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:28:50+00:00 2026-06-09T17:28:50+00:00

I am trying to create an element with ID of the element class name…

  • 0

I am trying to create an element with ID of the element class name…

my codes

//$elementClass has no id attrbute

          elementID=$elementClass.attr('class');

               $elementClass.first().attr('id',elementID);
               $element=$(elementID);               
               console.log($element);   

//the console.log doesn't output anything.... 

Can anyone help me about it? Thanks a lot.

  • 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-09T17:28:51+00:00Added an answer on June 9, 2026 at 5:28 pm

    Unless your class has the # character in it, all you get is the class name value. You need to add the # character to your selector if you want to select the element with that id.

    $element=$('#' + elementID); 
    

    However, seeing you are executing first() already to get the element of interest you might as well cache that object at that point and use it from there on.

    // Get the id value from the class name
    elementID = $elementClass.attr('class');
    
    // Create a jQuery object of the element I want to update
    var $element = $elementClass.first();
    
    // Update the element
    $element.attr('id',elementID);
    
    // Show the results in the console
    console.log($element);
    

    That should work too and it saves you having to concatenate selector values.

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

Sidebar

Related Questions

I am trying to create class properties for each element in an array. The
I am trying to create a select element that has a list of numbers
I'm trying to create a standardized show/hide element system, like so: <div class=opener popup_1>Click
I'm trying to create an element in an XML where the basic content is
I'm trying to create an element(div) on keyup of the space key (key-code 32).
I am trying to create a new Element in my javascript code and append
I'm trying to create a custom form element which extends Zend_Form_Element_Text with a validator
I'm trying to create a small html fragment (a div element) consisted of three
How to create a button that will overlay each html element with class WSEDIT.
Let's say an Owner has a collection of Watch(es). I am trying to create

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.