Home > Net >  Some Javascript codes not working, only jquery codes
Some Javascript codes not working, only jquery codes

Time:09-21

I have an external javascript file where I wrote jquery codes and javascript codes but the javascript codes aren't working only the alert is working.

I want some texts to flash different colors but it doesn't.

When I inspect elements using ctrl shift i I see the colors changing under inspect element, but it doesn't change in the webpage.

Something like it's running on the background or what I really don't know.

function flash() { var text = document.getElementById('sales'); text.style.color=(text.style.color=='red')? 'green' : 'red'; } var clr = setInterval(flash, 500)
<p id="sales"> <h3 style ="text-align: center; font-weight: bold;"> Flash Sales </h3></p>

CodePudding user response:

If you properly check the inspection, you'll see that your HTML was changed from what you have. A <p> element cannot contain a <h3> element, and the DOM has been changed to <p id="sales"></p><h3>...</h3>.

As such, your CSS is being applied to the wrong place.

function flash() { var text = document.getElementById('sales'); text.style.color=(text.style.color=='red')? 'green' : 'red'; } var clr = setInterval(flash, 500)
<h3 id="sales" style ="text-align: center; font-weight: bold;"> Flash Sales </h3>

CodePudding user response:

I was able to solve the issue by removing the in-line styling all the way from the <h3 tag and styled it in css.

CodePudding user response:

In pure java script

document.querySelector('element name').style.color = 'red';

In jquery

$('element name').css({red : 'red'});  
  • Related