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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:39:37+00:00 2026-06-05T14:39:37+00:00

I need to change an element’s id when I mouseover a different element in

  • 0

I need to change an element’s id when I mouseover a different element in a particular class. I then want to change the element id back to what it was previously on mouseout from the same element that I moused over before.

In the code below I propound a basic HTML code to clarify my question. I wish to change <div id="Set1"> to <div id="Set2"> onmouseover of any <div class="tomouseover">. Onmouseout from that same element I wish to change the original element’s id back to <div id="Set1">.

<div id="Set1">Div to change id</div>
<div class="tomouseover">Mouse over to change Set1 to Set2</div>
<div class="tomouseover">Mouse over to change Set1 to Set2</div>

+1 to ANY ideas! I’m royally stuck…

  • 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-05T14:39:40+00:00Added an answer on June 5, 2026 at 2:39 pm

    You can try Derek’s answer but you will soon discover that querySelector is not supported by IE8 in quirks mode or IE 7 and lower, and that addEventListener is not supprted by IE 8 and lower at all.

    Choices for plain js include writing your own getElementsByClassname function (not particularly difficult) or use event delegation so that you attach only one listener for each event and need a simple hasClass function.

    Anyhow, here’s an example:

    Some standard library functions that any javscripter should have in a library or be able to code in a few minutes:

    // Minimalist addEvent function, ok for the current web
    function addEvent(el, fn, evt) {
      if (el.addEventListener) {
        el.addEventListener(evt, fn, false) 
      } else if (el.attachEvent) {
        el.attachEvent('on' + evt, fn);
      } 
    }
    
    // Returns true or false depending on whether element el has class classname
    function hasClassname(el, classname) {
      var re = new RegExp('(^|\\s)' + classname + '(\\s|$)');
      return re.test(el.className); 
    }
    

    A function to do the work:

    // Toggle the id. Note that this deals with the case where
    // the element to toggle isn't found to prevent errors being thrown
    function toggleId(e) {
      e = e || window.event;
      var el = e.target || e.srcElement;
      var o;
    
      if (hasClassname(el, 'tomouseover')) {
    
        if (e.type == 'mouseover') {
          o = document.getElementById('set1');
    
          if (o) o.id = 'set2';
    
        } else if (e.type == 'mouseout') {
          o = document.getElementById('set2');
    
          if (o) o.id = 'set1';
        }
      }
    }
    
    // Attach the listener onload, could also add from a bottom
    // script or inline. If inline, use `toggleId(event)`
    window.onload = function() {
      addEvent(document.body, toggleId, 'mouseover');
      addEvent(document.body, toggleId, 'mouseout');
    }
    

    The above will work in all browsers back to IE6 and NN 3 or so and will continue to work in future browsers that are either W3C or IE event models.

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

Sidebar

Related Questions

What do I need to change in my code? Each element with the class
How can I change XML-element name for field inherited from base class while doing
I need to change the width of this element dynamically. So I want to
I need to change on the fly the class of an HTML element depending
I need change background of all text that have two spaces from the start
I use Pjax with tutorial from http://railscasts.com/episodes/294-playing-with-pjax?view=comments I don't need change url and this
I need to change more than one style attribute for a given element. I
When I need to change a text within a span element, which one should
<li id=sub:777 class=jstree-leaf> <ins class=jstree-icon2>&nbsp;</ins> <a class=><ins class=jstree-icon2>&nbsp;</ins>Story B</a> </li> I need to change
I need to change an element's ID using jQuery. Apparently these don't work: jQuery(this).prev(li).attr(id)=newid

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.