Member_2_1242703
asked on
Using jQuery to set a DIV attribute on page load
I have the following DIV
I want to set the value of "data-date" to the current date when the page loads. How would I do that?
<div class="input-group input-append date" data-date-format="mm-dd-yyyy" id="dp2" data-date="01/25/2017">
I want to set the value of "data-date" to the current date when the page loads. How would I do that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you need to include the jquery libraries, which i assumed already done as you requested this to be done with jquery...
ASKER
I do. I have other functions on the same page working fine.
on which line?
Your date string is wrong (line 6)
var strDate = (d.getMonth()+1) + d.getDate() + "/" + "/" + d.getFullYear() ;
- I think you meant this var strDate = (d.getMonth()+1) + "/" + d.getDate() + "/" + d.getFullYear() ;
Make sure your script is included AFTER the jQuery libraries
This works with the above amendment (http://www.marcorpsa.com/ee/t2060.html)
This works with the above amendment (http://www.marcorpsa.com/ee/t2060.html)
ASKER
Kind of strange here, I have my libraries referenced in a master page which works good, except for this (and others I presume?) Anyhow, I added the reference again to this page itself and the following works great.
<script type="text/javascript">
$(document).ready(function () {
$("#dp3").attr("data-date" , Date());
});
</script>
<script type="text/javascript">
$(document).ready(function
$("#dp3").attr("data-date"
});
</script>
How does this
mm/dd/yyyy
There is a double "/" + "/" in the string?
var strDate = (d.getMonth()+1) + d.getDate() + "/" + "/" + d.getFullYear() ;
Produce this?mm/dd/yyyy
There is a double "/" + "/" in the string?
ASKER
Open in new window
and got JavaScript runtime error: '$' is undefined