1

I'd like to put today's date into a textbox using javascript; Here is my code:

function add_event() {
    var currentDate = new Date()
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()
    var day1 = day + "." + month + "." + year

    var html = '<tr><td class="date"><input type="text" name="date_evnt"  value="?"></td> <td class="title"><input type="text" value="New Event"></td> <td class="delete"><input type="button" value="-"></td></tr>';
    $('#events-table').append(html);

    events_table_events();
}

I'm not sure how to set the date into textbox name date_evnt.

1
  • Which date do you want to go into the input? Also, may I suggest creating tr,td, and input nodes rather than raw HTML Commented May 30, 2013 at 2:51

2 Answers 2

3

If you just want to put the day1 value in date_evnt you can do this:

var html = '<tr><td class="date"><input type="text" name="date_evnt"  value="' + day1 + '"></td> <td class="title"><input type="text" value="New Event"></td> <td class="delete"><input type="button" value="-"></td></tr>';
Sign up to request clarification or add additional context in comments.

Comments

0

You may try this

function add_event() {
    var currentDate = new Date(), day = currentDate.getDate(),
        month = currentDate.getMonth() + 1, year = currentDate.getFullYear(),
        day1 = day + "." + month + "." + year;

    var txt1 = $('<input/>', { 'name':'date_evnt', 'value':day1 }),
        tr = $("<tr/>"), td = $('<td/>');

    tr.append(td.attr('class','title').html('New Event '))
    .append(td.append(txt1))
    .append(td.append($('<button/>', {'html':'-'})));

    $('#events-table').append(tr);
}
add_event();

DEMO.

Comments

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.