0

I try to change the dates in javascript.

So to start, in fact I have a mysql query that selects records from the database.

These records have two dates a date called action and another for validation.

In fact the script is supposed to change the dates on criteria but only if the validation date is equal to 0000-00-00 that is that the registration of the database is not considered valid.

So I actually added all the fields are not validated attribute required.

and I tried the code below I struggled to do:

<script type="text/javascript">
function lz(x){
return x.toString().replace(/^(\d)$/,'0$1')
}

    function addday() {
        var items = new Array();
        var itemCount = document.getElementsByClassName("datepicker hasDatepicker");


            items[0] = document.getElementById("date" + (1)).value;


            items[1] = document.getElementById("date" + (2)).value;


            items[2] = document.getElementById("date" + (3)).value;


            items[3] = document.getElementById("date" + (4)).value;


            items[4] = document.getElementById("date" + (5)).value;


            items[5] = document.getElementById("date" + (6)).value;


            items[6] = document.getElementById("date" + (7)).value;


            items[7] = document.getElementById("date" + (8)).value;


            items[8] = document.getElementById("date" + (9)).value;


            items[9] = document.getElementById("date" + (10)).value;



        for (var i = 0; i < itemCount.length; i++) {
            items[i] = document.getElementByAttribute('required').value;
Uncaught TypeError: Object #<HTMLDocument> has no method 'getElementByAttribute'
            var itemDtParts = items[i].split("-");
             var itemDt  = new Date(parseInt(itemDtParts[2],10), parseInt(itemDtParts[1],10)-1, parseInt(itemDtParts[0],10)+ +nb);
            nb=document.getElementById('nb').value;

                itemCount[i].value = lz(itemDt.getDate())+"-"+lz(itemDt.getMonth()+1)+"-"+itemDt.getFullYear()
        }


       return items;
           }

</script>

but I return the following error:

Uncaught TypeError: Object # Has No method 'getElementByAttribute'

I do not understand why.

the code that generates the javascript is as follows:

<script type="text/javascript">
function lz(x){
return x.toString().replace(/^(\d)$/,'0$1')
}

    function addday() {
        var items = new Array();
        var itemCount = document.getElementsByClassName("datepicker hasDatepicker");

  <?php  $sql2 = "SELECT * FROM agenda WHERE n_doss='".mysql_real_escape_string($_GET['n_doss'])."' AND qualite='".mysql_real_escape_string($_GET['qualite'])."' AND liasse='".$_GET['liasse']."' AND `date_validation`='0000-00-00' ORDER BY `date_action` ASC" ;
  $rules2=mysql_query($sql2) ; $i2=0;
  while($data2=mysql_fetch_assoc($rules2)) {?>

            items[<?php echo $i2 ; ?>] = document.getElementById("date" + (<?php echo ++$i2 ; ?>)).value;

<?php }?>


        for (var i = 0; i < itemCount.length; i++) {
            items[i] = document.getElementByAttribute('required').value;
            var itemDtParts = items[i].split("-");
             var itemDt  = new Date(parseInt(itemDtParts[2],10), parseInt(itemDtParts[1],10)-1, parseInt(itemDtParts[0],10)+ +nb);
            nb=document.getElementById('nb').value;

                itemCount[i].value = lz(itemDt.getDate())+"-"+lz(itemDt.getMonth()+1)+"-"+itemDt.getFullYear()
        }


       return items;
           }

</script>

I use this micro form to execute the function

<input type="button" value="( - )" width="22" height="22" onClick="subday()" />
                  <input name="jours" type="text" value="" size="5" id="nb" />
                  <input type="button" value="( + )" width="22" height="22" onClick="addday()"  />

Receive all my utmost Respect.

Kind regards.

SP.

2
  • That is because there is no such thing. Have a look here stackoverflow.com/questions/6267816/… Commented Oct 1, 2012 at 9:01
  • Dear Sir thank you verry much for your quick reply. Actualy I haver severa fields that have the criteria required like this one: for example: <input type="text" name="data[5][date]" class="datepicker hasDatepicker" id="date5" value="01-10-2012" size="12" style="background-color:#FF0" required=""> Commented Oct 1, 2012 at 9:04

1 Answer 1

1

There is no such thing as getElementByAttribute unless you make your own

XUL has a getElementsByAttribute but not available to normal JS AFAIK

Perhaps you wanted to do this?

function addday() {
  var items = document.getElementsByClassName("datepicker hasDatepicker");
  for (var i = 0; i < items.length; i++) {
    if (items[i].getAttribute('required')) {
      var itemDtParts = items[i].value.split("-");
      var itemDt  = new Date(parseInt(itemDtParts[2],10), parseInt(itemDtParts[1],10)-1, parseInt(itemDtParts[0],10)+ +nb);
      items[i].value = lz(itemDt.getDate())+"-"+lz(itemDt.getMonth()+1)+"-"+itemDt.getFullYear()
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Dear Sir thank you verry much for your quick reply. I've tried using the code you gave me, but it does not change anything. I added a return items; at the end

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.