locked
Javascript for each datalist label RRS feed

  • Question

  • User-431147622 posted

    I have a javascript which is getting the current time and i have to add days(each asp.net field contains a different number of days) which is being stored in my mssql db.

    Now my function is working fine but its working for the 1st record only. 

    Any help to make it work for each Date Ending.... Any help.... 

    <script type='text/javascript'>
    window.onload = function () {
    var today = new Date();
    var newdate = new Date();

    var getnumofdays = (document.getElementById("lblBidDuration").innerHTML);

    newdate.setDate(today.getDate() + parseInt(getnumofdays) );
    var dateformat = (newdate.getMonth() + 1) + " " + newdate.getDate() + " " + newdate.getFullYear() + " " + newdate.getHours() + ":" + newdate.getMinutes() + ":" + newdate.getSeconds()
    var d = new Date(dateformat)
    var convertdate = d.toLocaleDateString('en-GB', {
    month: 'short',
    day: 'numeric',
    year: 'numeric'

    });

    var converttime = d.toLocaleTimeString('en-GB', {
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
    })

    var convertedtime = convertdate + ',' + converttime;
    document.getElementById("lblDateEnding").innerHTML = convertedtime


    }
    </script>

    Tuesday, January 28, 2020 9:25 AM

All replies

  • User-474980206 posted

    element id's are supposed to be unique, so document.getElementById() always return the first match, so you need to make each id unique. a common pattern is name_<index>_.  do a loop incrementing index. either render the number of rows or stop when not found. 

    Tuesday, January 28, 2020 9:33 PM