Stuck with JavaScript function when generating number

131 Views Asked by At

I apologize if this code is a complete mess, but I'm attempting to create a function that will generate an RMA for customers when they call in for support, I've got all the element ID's right but I can't figure out why it doesn't output the result I'm looking for its should be something like this

RMA # = month + day + year + user id + ticket #

Formatted Answer = (06232015)(555)(123)

Plaintext = 06232015555123

<div id="complete_rma">
</div>
<script type="text/javascript">
function RMA(){
var date = new Date();
var d = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
var agent = document.getElementById("helpdesk_ticket_responder_id").value;
var ticket = document.getElementById("ticket-display-id").innerHTML;
var rma_number = (day, month, year, agent, ticket);
document.getElementById("complete_rma").innerHTML = RMA;
}
</script>

any help and criticism would be appreciated, this is my first foray into javascript and I can't wrap my head around this

2

There are 2 best solutions below

4
On BEST ANSWER

You have done number of mistakes in the code. See the fiddle for working example, you have to do small changes as you need. http://jsfiddle.net/2bvyna22/

function RMA(){
    var date = new Date();
    var d = date.getDate();
    var day = (d < 10) ? '0' + d : d;
    var m = date.getMonth() + 1;
    var month = (m < 10) ? '0' + m : m;
    var yy = date.getYear();
    var year = (yy < 1000) ? yy + 1900 : yy;
    var agent = document.getElementById("helpdesk_ticket_responder_id").value;
    var ticket = document.getElementById("ticket-display-id").innerHTML;
    var rma_number = "("+day+", "+month+", "+year+", "+agent+", "+ticket+")";
    document.getElementById("complete_rma").innerHTML = rma_number;
}

Fiddle updated with innerHTML for ticket. http://jsfiddle.net/2bvyna22/1/

0
On

You are close...

Your rma_number assignment needs to utilize the string concatenation operator, + (rather than commas).

And your last line should reference the rma_number variable rather than RMA...

function RMA() {
  var date = new Date();
  var d = date.getDate();
  var day = (d < 10) ? '0' + d : d;
  var m = date.getMonth() + 1;
  var month = (m < 10) ? '0' + m : m;
  var yy = date.getYear();
  var year = (yy < 1000) ? yy + 1900 : yy;
  var agent = document.getElementById("helpdesk_ticket_responder_id").value;
  var ticket = document.getElementById("ticket-display-id").innerHTML;
  var rma_number = ('' + day + month + year + agent + ticket);
  document.getElementById("complete_rma").innerHTML = rma_number;
}