2

I'm writing a script that will split the element's ID from the format 'note-1192' (for example) and place it into an array:

    var timers = new Array();

    $('.notes').keyup(function(){

        var timerVariable = $(this).attr('id').split("-");
        timerVariable = timerVariable[0];
        timerVariable = timerVariable.replace('note', '');
        alert(timerVariable); // 1192

        timers[timerVariable] = timerVariable;
        alert(timers.join('\n')); // blank

    });

It's not storing the variable into the array at all, when I alert the 'timers array' it's empty, but when I alert the 'timerVariable' it comes out just fine.

Any suggestions would be very welcome, thanks!

4
  • 1
    are you sure its blank? because by inserting at index 1192, you are making 1191 undefined spots before your entry. which means when you join() there are going to a a lot of empty lines before your data. Commented Jan 17, 2012 at 16:54
  • 1
    Based on the first 4 lines of your function, if timerVariable = 'note-1192', alert(timerVariable) will never show 1192. The first index of the array timerVariable[0] will contain 'note' which will be replaced with '' after this line: timerVariable = timerVariable.replace('note', ''). Commented Jan 17, 2012 at 16:57
  • 1
    Hold on. var timerVariable = $(this).attr('id').split("-"); so now timerVariable is an array, with 'note' at position 0 and '1192' at position 1. Then, timerVariable = timerVariable[0]; so now timerVariable is just 'note'. How did you end up with 1192 when you alerted? Commented Jan 17, 2012 at 16:58
  • The sample code is neither complete, nor concise nor (if the comment is to be believed) representative. If you had produced a complete, concise, representative sample, you probably would have spotted the error. Commented Jan 17, 2012 at 17:08

6 Answers 6

5

The problem is the index syntax your are using is setting a named property on the array object not adding an element. To add an element to the array use push

timers.push(timerVariable);
Sign up to request clarification or add additional context in comments.

5 Comments

i am pretty sure that a["1"] and a[1] are the same. but i'm willing to be wrong on that.
but you are right that a[" 1"] != a[1], so i'm gonna go with a +1, given that its not a requirement to insert at the specific timer index.
@32bitkid yes that's true. The index of an array with a number as a string works just fine. In this case though the OP is using a value which is not expressable as a number
Many thanks, this works perfectly, I think - I'm not sure if my array is at fault, or the code that calls it, becuase this code isn't clearing the timer correctly - pastebin.com/E6ABCa6J
Array indices are strings, not numbers. a["1"] is the actual result of doing a[1].
0

try

Array:

var timers = new Array();
timers.push(timerVariable);

Hash:

var timers = {};
timers[timerVariable] = timerVariable;

Comments

0

Use push method of array to add an element to array.

timers.push(timerVariable);

Comments

0
var timers = new Array();

$('.notes').keyup(function(){
    timers.push($(this).attr('id').split("-")[0]);
});

Comments

0

Despite the assertion in the comment,

"note-1192".split("-")[0].replace("note", "") === ""

The simple fix would be to get the second, rather than first, element of the split string:

var timerVariable = $(this).attr('id').split("-")[1];

To ensure you get a number, you could use a regular expression.

var timerId = $(this).attr('id').match(/\d+/);
if (timerId) {
    timerId=timerId[0];
    ...
}

Comments

0

I think this is what you are trying to do:

var timers = new Array(); 

$('.notes').keyup(function(){ 

    var temp = $(this).attr('id').split("-"); 
    timerValue = temp[0]; 
    timerID = temp[1];
    alert(timerID); // 1192 

    timers[timerID] = timerValue; 
    alert(timers.join('\n'));

}); 

With the way you currently have it, timerVariable will be '' in the end, and so what you're doing at the end is really the same as:

timers[''] = '';

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.