-2

I would like to change the style of another inside a html element using javascript.

I have used the below code to change the current html element.

<p onmouseover="this.style.color = 'black;">This text should be changed</p>
<h1>How to change this element when hovered on p element</h1>

I would like to change the other element's style inside the p tag using javascript.

can anyone help?

3
  • Possible duplicate of Add inline style using Javascript Commented Feb 10, 2017 at 9:55
  • not clear with the question... Commented Feb 10, 2017 at 9:55
  • why use js simple use with css p:hover + h1{"add your style} Commented Feb 10, 2017 at 9:59

7 Answers 7

1

Using CSS you can achieve the same

<style>
 p:hover + h1 {
  background-color : red
 }
</style>
Sign up to request clarification or add additional context in comments.

Comments

0

This should be what you're after (not my work) - check out the fiddle link ...

<html>
<body>
    <span id="div1" style="color:black;" onmouseover="stext()" onmouseout="htext()">TEXT1</span><p />
    <hr color="black" />
<span id="div2" style="color:red;" onmouseover="htext()" onmouseout="stext()">Text2</span> 

</body>

http://jsfiddle.net/FFCFy/16/

Comments

0

for example if you want to change the color:

<script>
document.getElementByTagName("p").style.color = "blue";
</script>

that should probably bound to an event, accordingly to what you want to do

Comments

0

use this :

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <p style="color:red;" onmouseover="ch(event)">This text should be changed</p>
    <h1>How to change this element when hovered on p element</h1>

    <script>
        function ch(e) {
            e.target.style.color = "black";
            alert();
        }
    </script>

</body>
</html>

Comments

0

use with Javascript

function change(that){
document.getElementsByTagName('h1')[0].style.color="red";
  
  }
<p onmouseover="change()">This text should be changed</p>
<h1>How to change this element when hovered on p element</h1>

use with css

p:hover + h1{
  color:red;
  }
<p >This text should be changed</p>
<h1>How to change this element when hovered on p element</h1>

Comments

0
jQuery("p").mouseover(function(){
   jQuery("h1").css("color", "yellow");
});

Comments

0

You can easily achieve it with jQuery:

$(function() {
  $('#a-element').hover(function() {
    $('#b-element').css('background-color', 'yellow');
  }, function() {
    // on mouseout, reset the background colour
    $('#b-element').css('background-color', '');
  });
});

If #b comes immediately after #a, the simplest solution is in pure css:

#a:hover + #b {
    background: #ccc
}

If between #a and #b are other elements, you have to use ~ like this:

#a:hover ~ #b {
    background: #ccc
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.