0

I have this below code where i am trying to alert the date selected on click of an image. I am getting the alert but the issue is date format.

Below is alert i am getting. I need the alert in mm/dd/yyy

Below is the javascript code -

<script>
var count = 0;

function myFunction() {
    var a = document.getElementById("page:j_id1:myPicker1").value;
    var a1 = document.getElementById("page:j_id1:myPicker2").value;
    var str = "";

    count++;

    str+="<div class='label' date1='"+a+"' date2='"+a1+"'>"+count +". Date 1 "+a;
    str+="Date 2 "+a1;
    str+="<a href='#' onclick='fnUpdateDate(\'"+a+"\',\'"+a1+"\')'>"; 
    str+="<img src='https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQlvCNq-gyHx63VKBLy1H-lHnqRar0jKJqkoCP9giVm5y76NVY-jg' width='48' height='48'/>";
    str+="</a>";
    str+="</div>";

    $("#resultContainer").append(str);
}


function fnUpdateDate(date1,date2){
    alert(date1+" ss "+ date2);
}
</script>

enter image description here

<div class="label" date1="10/4/2016" date2="10/12/2016">1. Date 1 10/4/2016Date 2 10/12/2016<a href="#" onclick="fnUpdateDate(10/4/2016,10/12/2016)"><img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQlvCNq-gyHx63VKBLy1H-lHnqRar0jKJqkoCP9giVm5y76NVY-jg" width="48" height="48"></a></div>

Regards

1 Answer 1

0

As you now clarified your code and the values of the variables, I found the problem in your code. You need to add (escaped) Quotes to the strings of a and a1:

str+="<a href='#' onclick='fnUpdateDate(\""+a+"\",\""+a1+"\")'>";

And as your date-strings are in the correct format you can skip creating the Date object:

function fnUpdateDate(date1,date2){
    alert(date1+" ss "+ date2);
}
4
  • Hi Markus.. I am getting error in console ...Unexpected token }. I have updated my code above.. Commented Oct 7, 2016 at 18:50
  • I updated the answer some moments ago (I used the wrong quotes). The code should work now. Commented Oct 7, 2016 at 18:50
  • Thanks .. I got the correct format now.. What was the issue.. Commented Oct 7, 2016 at 18:53
  • You put an unquoted string value inside a function call, so the date string "10/4/2016" wasn't interpreted as a string. Instead, the slashes were interpreted as division signs and the value was calculated. Commented Oct 7, 2016 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .