1
function get_event_ids_from_dom()
{
    var event_ids = {};
    $.each(
    $("td.ms-cal-defaultbgcolor a"),
        function(index,value){
             var str = new String(value); 
             var id = str.substring(str.indexOf('=')+1,str.length);
             if(typeof(event_ids[id]) == "undefined")
             {
                event_ids[id] = this;
            }
            else
            {
                **event_ids.id.push(this);**

            }
        }
     )
        return event_ids;
}

In above javascript event_ids is a hashtable. I am trying to assign values to this hashtable.

A hashtable can be added with multiple values using "hashtable.key.push(value)". I am trying to do this using event_ids.id.push(this); in the above code.

I have declared "id" as a variable in the code. The problem is, I am not able to dereference variable "id" to its value.

Is this possible in jquery/javascript?

Example use of hashtable:

event_ids = {};
event_ids["1"]= 'John';
event_ids.1.push('Julie');

The above example would add john and julie to hash table.

4
  • @Ivaylo Slavov No it does not work. I am getting TypeError: event_ids[id].push is not a function, In firebug. Could you please check again? Commented Jun 23, 2011 at 14:19
  • 1
    Your example is not valid JavaScript: **event_ids.id.push(this);** ? And you shouldn't use the String class directly. It's just there to decorate string values with methods. When you call methods on a string value JavaScript is simply calling the methods on the prototype of String in the context of the string value. You're going to run into problems if you're using an object as a string instead of just a string. Commented Jun 23, 2011 at 14:24
  • @apphacker The value variable will have something like this "Business%20Development/Lists/Calendar/DispForm.aspx?ID=907". But it is in object form. I want to convert it into string to process it. Is it possible without using String class? Commented Jun 23, 2011 at 14:33
  • @apphacker got it from Ivaylo's answer. changed my code to value.toString() , Thanks for the info :-) Commented Jun 23, 2011 at 14:43

3 Answers 3

2

Try this instead:

function get_event_ids_from_dom() {
    var event_ids = {};
    $.each(
        $("td.ms-cal-defaultbgcolor a"),
        function(index,value){
            var str = value.toString(); 
            var id = str.substring((str.indexOf('=') + 1), str.length);
            if(typeof(event_ids[id]) == "undefined") {
                event_ids[id] = [];
            }
            event_ids[id].push(this);
        });
    return event_ids;
}

Please, note that while object["id"] is the same as object.id, object[id] is not.

Sign up to request clarification or add additional context in comments.

2 Comments

You should accept his answer if you like it. Also Ivaylo I can't vote up your answer until you get rid of that String use. :P
@apphacker, thanks for pointing out, it was a copy-paste error.
2

Nicola almost had it:

if(typeof(event_ids[id]) == "undefined") {
  event_ids[id] = [];
}
event_ids[id].push(this);

Also please read the comment I left for your question.

1 Comment

yes, stupid error i made. I corrected it. I think in his question he was trying to put bold around the line.
1

In my opinion event_ids is an object (there are no hastables in javascript, just either indexed arrays or objects). What you are tring to do is using push (an array method) on something that is not an array so i think you must change something:

you could try:

         if(typeof(event_ids[id]) == "undefined")
         {
            event_ids[id] = [];// the property id of object event_ids is an array
            event_ids[id].push(this);
        }
        else
        {
            event_ids[id].push(this);

        }

It should work

3 Comments

event_ids[id][] = this; is not valid JavaScript.
The else statement is totally unnecessary.
i didn't want to copy your answer! :) i just put correct code inside, it sure is unneccessary

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.