views:

87

answers:

6

New to Javscript and jQuery, so I assume this is a pretty simple question.

I have a date I want to split into a string, then print out the array.

var date = "12/10/2010";
var dateArray = date.split(/);

$('#printbox').html($(dateArray[0]));

<span id="printbox"></span>


Edit: The question is, how can I split "12/10/2010" into an array like this:

Array[0] = 12
Array[1] = 10
Array[3] = 2010

And how do I print them to HTML?

A: 
var date = "12/10/2010";
var dateArray = date.split(/\//); // have to quote regular expressions with /

document.write; // this does nothing

$('#printbox').html(dateArray[2] + '-' + dateArray[0] + '-' + dateArray[1]); // do what you want here. The code I provided should print 2010-12-10
cjavapro
I don't see the point of regex in `.split()`, doing .split('/') is simpler and does the exact same thing.
indieinvader
A: 

For splitting you were almost there :)

var dateArray = date.split("/");

If you want to use jQuery you could use the each method.

$.each(dateArray, function(index, value) { 
  // print out here
});

This constructs a loop that iterates over the elements in your array. You can access this element by the value variable.

willcodejavaforfood
+5  A: 

The first parameter to the string.split function should be a string or regex:

var date = "12/10/2010";
var dateArray = date.split("/");

// dateArray[0] == 12
// dateArray[1] == 10
// dateArray[2] == 2010

// Now use the array to print out whichever way you want (jQuery.html(values))...
Oded
This seems nice, the Array is just not outputting to HTML.
danixd
Out of curiosity, what does putting the first element of the array into a jQuery construct do? Does it print out all values of the array? I haven't seen this before.
stjowa
I realize this is a carryover from the question, but it doesn't make sense to wrap `dataArray[0]` in a jQuery object. :o) Better would be to do a `.join()`, like `.html( dateArray.join(', ') )`.
patrick dw
@stjowa - if you look at the example of the OP, the HTML markup has a div with id `printbox`, the jQuery simply sets the contents of that div with the passed in string. See [`.html`](http://api.jquery.com/html/)
Oded
@danixd - it was taken from your example. The jQuery [.html](http://api.jquery.com/html/) function will populate the selected elements with the passed in html.
Oded
it creates DOM elements out of the string. The call to $('#printbox')'s html is setting the inner html of the #printbox element to the month in the date array. see more here: http://api.jquery.com/jQuery/#jQuery2
Tim Snowhite
@Oded - I was referring to $(dateArray[0]). This would have translated to $(12) and didn't make sense as **patrick dw** described. But, I see it got removed.
stjowa
@stjowa - Fair enough, guess I didn't spot the wrapping of the array in jQuery first time around.
Oded
Thanks for the help, worked a treat.
danixd
+2  A: 
var date = "12/10/2010";
var dateArray = date.split('/');

$('#printbox').html(dateArray[0]); // it displays 12 in printbox
$('#printbox').html(dateArray[1]); // it displays 10 in printbox
$('#printbox').html(dateArray[2]); // it displays 2010 in printbox
Spilarix
Each subsequent call to `.html()` will overwrite the previous.
patrick dw
Of course ! It was to help him to make a choice. It was obvious for me but you're right, not for everybody...
Spilarix
Ok, I see what you meant. +1
patrick dw
A: 
var date = "12/10/2010";
var dateArray = date.split('/');

var year = dateArray.splice(2,1);
dateArray[3] = year;

should give you

Array[0] = 12
Array[1] = 10
Array[3] = 2010

But why you need the year in the fourth key I don't know. (also removes from the third so you don't end up with two years)

BenWells
A: 
var date = "12/10/2010";
var dateArray = date.split('/');

// dateArray => ['12', '10', '2010']

If you want to use jQuery there is an each function that you can use:

$.each(dateArray, function (index, value) {
    $('#printbox').append(value);
});

There is also a .forEach method in newer browsers:

dateArray.forEach(function (value) {
    document.getElementById('printbox').
        appendChild(document.createTextNode(value));
});

Older browsers can have this method via the Array.prototype:

if (!(Array.prototype.forEach === 'function')) {
    Array.prototype.forEach = function (fn) {
        for (var i = 0, l = this.length; i < l; i += 1) {
            fn(this[i]);
        }
    };
}
indieinvader