1

I have the following script:

http://jsfiddle.net/oshirowanen/gcYeB/

How do I split the string being displayed in the alert box into 2?

So I get an alert box 1 after the other.

The first alert showing

ABC

and the second showing

123

Fokko Driesprong
  • 2,075
  • 19
  • 31
oshirowanen
  • 15,297
  • 82
  • 198
  • 350
  • possible duplicate of [How do I split this string with javascript?](http://stackoverflow.com/questions/96428/how-do-i-split-this-string-with-javascript) – Felix Kling Mar 03 '11 at 14:40

2 Answers2

7

You can use the Javascript split function.

$('.test').live('click', function() {
    var id = $(this).attr("id"));

    var parts = id.split('_');
    alert( parts[ 0 ] );
    alert( parts[ 1 ] );
});
Fokko Driesprong
  • 2,075
  • 19
  • 31
0

Split the id using the split method and the "_" as a separator. Then, repeat over each token ($.each) and alert its value.

$('.test').click(function() {
    $.each($(this).attr("id").split("_"), function(key, value) {
        alert(value);
    }) 
 });
Ryan Miller
  • 391
  • 1
  • 12