1

I am working on a project involving Javascript and I need a string to be underlined. The code has the word "Date" in front of the current date (represented by the variable now). The current date needs to be underlined, not the word "Date".

This is the code I am using:

var now = new Date();
document.getElementById('date').innerHTML = "Date " + (now.getMonth()+1)+ "/"+ now.getDate()+ "/"+ now.getFullYear();

How can I do this?

1 Answer 1

4

When something with the ID of "date" is found...

Use CSS for presentation:

#date {text-decoration: underline;}

Snippet

var now = new Date();
document.getElementById('date').innerHTML = "Date <span>" + (now.getMonth()+1)+ "/"+ now.getDate()+ "/"+ now.getFullYear() + "</span>";
#date span {
  text-decoration: underline;
}
<div id="date"></div>

Also, please note, there should not be any duplication of ids. So if there are multiple date elements in the same page, use class instead and style this way:

.date {text-decoration: underline;}

Preview

preview

Sign up to request clarification or add additional context in comments.

14 Comments

One issue. The entire string is underlined if you do it this way. I want just the date underlined, not "Date" and the date.
@AChildofGod You have to enclose the Date inside, say <u> tag?
I am also using a span tag with an ID of "date" to display it. It works. Had I wanted the entire string to be underlined, I wouldn't be asking a question. I could easily style it with the span tag.
@AChildofGod Change your code to: .innerHTML = "<u>Date</u> " +
@AChildofGod You are using full line without any differentiation to the Date. See the above comment and try changing your code accordingly.
|

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.