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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:42:47+00:00 2026-06-03T05:42:47+00:00

In jQuery you can create new elements and define properties on them in one

  • 0

In jQuery you can create new elements and define properties on them in one step:

$("<div>", {title: "Hey there!", text: "I'm a DIV!"});

This produces:

<div title="Hey there!">I'm a DIV!</div>

Is there a way to do the same with an existing element? The following does not work:

$("#theDiv", {title: "Hey there!", text: "I'm a DIV!"});
  • 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-03T05:42:48+00:00Added an answer on June 3, 2026 at 5:42 am

    Turns out things are rather easy. jQuery uses .attr() for this.

    $("#theDiv").attr({title: "Hey there!", text: "I'm a DIV!"}, true);
    

    Note the second parameter set to true. This causes jQuery to use the same mechanism that it uses for freshly created elements.

    Even function parameters work as expected:

    $("#theDiv").attr({
        title: "Hey there!", 
        text: function () { return "some calculated value"; }
    }, true);​
    

    (See this jsFiddle.)

    Consequently, this will also work for event bindings, CSS modifications and all the other things jQuery can do:

    $("#theDiv").attr({
        title: "Hi there", 
        text: function (i, v) { return v.replace(/not /, ""); },
        mouseover: function () { $(this).css({color: "red"}) },
        mouseout: function () { $(this).css({color: ""}) },
        css: { fontSize: "20pt", fontStyle: "italic" }
    }, true);​
    

    (See this jsFiddle.)

    This is undocumented, so it could change in any dot release without being listed in the release notes, but it seems unlikely to and it’s easy to test it when upgrading jQuery.

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

Sidebar

Related Questions

I need to position a div over an image with jQuery. I can create
Is there any way that I can use JavaScript (and jQuery) to create a
I'm creating DOM elements dynamically (more specifically, using jQuery to create a DIV containing
In jQuery Mobile, we can easily create a Back button with data-rel=back or data-add-back-btn=true
I have been trying to create a menu panel with jQuery that can be
I'm using the plugin http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/ to create tooltips with JQuery but I can't created
JQuery - Can we capture both the p and div tags at a time
I have my form elements here: <li><div id=formanswer1 class=clonedInput>Answer: <input type=text id=formanswer1 value= /></div></li>
The normal behavior for the jQuery qTip plugin is to create a new hidden
I have multiple element that are jquery ui draggable, these elements can also be

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.