2

I am new to programming and I was wondering out of the below written two ways which one is more preferable/better approach and why?

CODE is to add a given number of days to a given date and insert the new date in an input box on selection of number of days from a list of radio buttons

HTML

Old Date (MM/DD/YYYY) : <input type="text" id="date1" /><br><br>
<input type="radio" name="expiry" id="allot" value="5" onchange="dateChange()">5 Days
<input type="radio" name="expiry" id="allot" value="10" onchange="dateChange()" >10 Days <br> <br>
New Date (MM/DD/YYYY) : <input type="text" id="date2"/><br><br>

jQuery (first)

function dateChange(){
  var days = $('input[type=radio][name=expiry]').val();
  var myDate =new Date($("#date1").val());
  myDate.setDate(myDate.getDate()+parseInt(days));
  var inputDate = (myDate.getMonth()+1)+'/'+(myDate.getDate())+'/' (myDate.getFullYear());
  $("#date2").val(inputDate);
}

jQuery (second)

$(document).ready(function() {
$('input[type=radio][name=expiry]').change(function() {
    var d = this.value;
    var myDate=new Date($("#date1").val());
    myDate.setDate(myDate.getDate()+parseInt(d));
    var inputDate = (myDate.getMonth()+1)+'/'+(myDate.getDate())+'/'+(myDate.getFullYear());
    $("#date2").val(inputDate);
});});

Thank you in advance.

Mhluzi Bhaka
  • 1,364
  • 3
  • 19
  • 42
phantom
  • 43
  • 8
  • 1
    Second approach is better. Its keeps HTML clean as inline event handler are not there. In short separation of presentation and logic. – Satpal Aug 02 '15 at 06:15
  • 1
    is clean HTML is the only difference ? – phantom Aug 02 '15 at 06:19
  • There are other difference which is already discussed in these question. [onclick=“” vs event handler](http://stackoverflow.com/questions/6941483/onclick-vs-event-handler), [jQuery.click() vs onClick](http://stackoverflow.com/questions/12627443/jquery-click-vs-onclick) – Satpal Aug 02 '15 at 06:20

0 Answers0