Forum Moderators: open

Message Too Old, No Replies

Change 'id' value to change ':hover' pseudo-class value

         

Bennie10

11:12 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



Dear Webmaster World,

Maybe the method below for changing an element's CSS ":hover" pseudo-class value, using JavaScript, is common knowledge--but I wasn’t able to find anything like it on the Internet. And, if there is an easier method to accomplish the same thing, please let me know...

I was able to replace the text in a 'paragraph' element with new text using a JavaScript 'onclick' event. But, I was unable to change the paragraph's CSS ':hover' pseudo-class declaration value using the 'HTML DOM Style Object'--as you would to change a text's 'color' declaration value, for example--so that the new text would have a different color when hovered-over by a cursor than the original text had.

Then, it dawned on me that if I could change the paragraph's 'id' value during the 'onclick' event, this new 'id' value could point to a different CSS 'selector' with a different ":hover" pseudo-class value (color) assigned to it.

After typing 'changing element's "id" value' into my search-engine, I discovered a "thread" on your Forum by somebody named Tiibou

(#:1482655 - [webmasterworld.com ]),

who used an 'onclick="this.id='...';"' event to change an element's 'id' value.

So, I added the "this.id='...';" part of Tiibou's script to my script's 'onclick' event...and, voila!...the ":hover" color of the paragraph's new text changed!

I made sure to use the newly created 'id' value in the function that was called by the 'onclick' event.

Here, for anyone who is interested, is the code that will change an element's CSS ":hover" value using JavaScript:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<style type="text/css">

#click1:hover {
color: red;
}

#click2:hover {
color: blue;
}

</style>

<script type="text/javascript">

// <![CDATA[

function change_text() {

document.getElementById('click2').innerHTML="Thank You";

}

// ]]>

</script>

</head>

<body>

<span id="click1" onclick="this.id='click2'; change_text();">Click Here</span>

</body>

</html>


Thank you Tiibou, and thank you Webmaster World for this Forum.

Sincerely,

Bennie10

daveVk

1:50 am on Jul 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Minor simplification

function change_text(el) {
el.innerHTML="Thank You";
}

AND

<span id="click1" onclick="this.id='click2'; change_text(this);">Click Here</span>

To make the meaning of the code more obvious you could change the class name rather than the id

<span class="click1" onclick="this.className='click2'; change_text(this);">Click Here</span>

Should the span still need an id, then you code no longer needs to consider what it might have been changed to.

Bennie10

2:40 am on Jul 1, 2010 (gmt 0)

10+ Year Member



Dear DaveVk,

Your suggestions make a lot more sense. I'm going to make those changes. Thanks.

I'll have to look into that "this" part, too. I'm a little fuzzy as to its use.

Also, am I correct in assuming that there is no direct way to change a ":hover" value using JavaScript, like you can with a text-color? [ex: document.getElementById("id_here").style.color="#hex_number_here";]

Bennie10

daveVk

5:30 am on Jul 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do not know of a more direct way.

"this" refers to the element you are on, like element returned by getElementById("id_of_current_element").

Bennie10

5:43 am on Jul 1, 2010 (gmt 0)

10+ Year Member



Dear DaveVk,

Thank you for your help.

Bennie10

Bennie10

4:28 pm on Jul 1, 2010 (gmt 0)

10+ Year Member



Dear Forum Members,

For those of you out there who are relatively new to JavaScript and coding in general, as I am (2.5 years), here is a version of my original script that incorporates the changes that Dave suggested, and places the JavaScript 'statement' that was in the 'onclick' event up into the 'change_text' function:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<!-- For the evolution of this script, go to: www.webmasterworld.com/javascript/4162426.htm -->

<style type="text/css">

.click1:hover {
color: red;
}

.click2:hover {
color: blue;
}

</style>

<script type="text/javascript">

// <![CDATA[

function change_text(el) {

el.className='click2';
el.innerHTML="Thank You";

}

// ]]>

</script>

</head>

<body>

<span class="click1" onclick="change_text(this);">Click Here</span>

</body>

</html>

Sincerely,

Bennie10