Can't get function variable to split.

Discussion in 'JavaScript' started by loquaci, Oct 5, 2010.

  1. #1
    The function variable 'data' is formatted like this: 0,70; but I need to separate this at the comma so I have two different variables from the one string. I'm trying to do this using split() but it's not working and stops the rest of the code from working. I don't know any alternatives to split other than substr, and it is not working either.

    
    $('#chart').bind('jqplotDataHighlight', function(ev, seriesIndex, pointIndex, data){
    var myArray = data.split(',');                    
    $('#tooltip').text('This is your score: ' + myArray[0]);
    });
    
    Code (markup):
     
    loquaci, Oct 5, 2010 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It seems fine, so I think the problem is somewhere else. This example might help you spot it:
    
    <html>
    <head>
    <script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.min.js'></script>
    <script type='text/javascript'>
    	$(document).ready(function() {
    		$('#chart').bind('jqplotDataHighlight', function(ev, seriesIndex, pointIndex, data) {
    			var myArray = data.split(',');  
    			$('#tooltip').text('This is your score: ' + myArray[0]);
    		});
    		$('button').click(function () {
    			$('#chart').trigger('jqplotDataHighlight', [ 0, 0, '0,70;' ]);
    		});
    	});
    </script>
    </head>
    <body>
    <div id='chart'>Chart</div>
    <p id='tooltip'>Tooltip</p>
    <button>Trigger Event</button>
    </body>
    </html>
    
    Code (markup):
     
    Cash Nebula, Oct 5, 2010 IP