Placing Javascript in Div

Asked

Viewed 103 times

0

I have this code to show the date

<script language="javascript" type="text/javascript">// <![CDATA[
now = new Date
document.write ("Esquenta Black Friday Somente Hoje  " + now.getDate() + " do " + now.getMonth() + 1, )
// ]]></script>

Needed it inside a div to use these CSS settings

.promocoes-kit {

background: #fff1dc;

 width: 80%;

 height: 50%;

  margin: auto;

 border-radius: 10px;

border: solid 4px white;

box-shadow: 3px 3px #bdbcbc5e;
}

.promocoes-kit{

 font-size: initial;

text-align: center;

 font-weight: 600;

 color: #ea9b2a;

  width: fit-content;

 padding: 10px 30px;
}

.promocoes-label{

 padding: 0;

  margin: 0;

 text-align: center;

 font-size: large;

 color: #ea9b2a;

 font-weight: 600;
}

Can anyone tell me how to do it? I will use it on a LADING PAGE as a banner on Magento

1 answer

2


From what I understand you don’t have much knowledge of Javascript, then follows some considerations:

1- You are using Document.write which is not indicated because it overwrites all its HTML.

2- Always the most indicated is you declare variables with the keyword var or Let, for N motives.

3- When you do it now.getMonth() + 1, you will have the previous month plus one or be 101 and it would be right to put inside parentheses to form an expression like this (now.getMonth() + 1).

var div = document.getElementById('teste');
var datas = new Date();

div.innerHTML = 'Esquenta Black Friday Somente Hoje  ' + datas.getDate() + ' do ' + (datas.getMonth() +1);
body {
  background-color: #CCC;
}

#teste {
  background-color: green;
  color: white;
}
<div id="teste"></div>

  • 1

    Man Thank you so much for the explanation, perfect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.