2

There is a string value in a p class like <p id="demo">10 hours : 25 minutes : 20 seconds</p>. Here I want to split that value like 10:25:20. This is my javascript function.

function countdownTimeStart(){
   var countDownDate = new Date("Mar 5, 2018 15:37:25").getTime();

   var x = setInterval(function() {
     something ... 
   }

So I need to replace the var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime(); into above 10:25:20 which comes from p tag. I'm tried following way.

var countDownDate = document.getElementById("demo").value;

But it does not work. Can anyone help me to solve this.

3 Answers 3

4

You need to access the text content of the element there. .value only works for inputs and textareas.

const digits = document.querySelector('#demo').textContent.match(/\d\d/g);
const time = digits.join(':');
console.log(time);
<p id="demo">10 hours : 25 minutes : 20 seconds</p>

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

4 Comments

Where is the var countDownDate object?
Can you please tell me how can I apply this into var countDownDate, I'm new to the Javascript.
Just replace const time with const countDownDate if you want the variable name to be countDownDate.
"not working" does not tell me anything about what the problem is - please elaborate.
2

div doesn't have a value, it has innerHTML and textContent which can be get/set

var countDownDate = document.getElementById('demo').textContent;

Comments

0

Use document.getElementById("some");

        var x = document.getElementById("some").innerHTML;


</script>
<body>
    <p id="some">Some text</p>
</body>

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.