0

i need to get the array id and put this id number in the var pointerid = (id number from array);
i am using this code to store the cliked id from element:

jQuery('a').click(function(){
    var clickedId= $(this).attr("id");

now i want to search if 'element' : 'Equals to clickedId', if yes, get array id. for example:
clickedId= test
wp_button_pointer_array[1] = { 'element' : 'test'
so here 'element' : 'test' = clickedId (test) then give me the array id. array id here is 1

 var wp_button_pointer_array = new Array();
 wp_button_pointer_array[1] = {
     'element' : 'wp-button-pointer',
     'options' : {
         'content': 'The HTML content to show inside the pointer', 
         'position': {'edge': 'top', 'align': 'center'} 
     } 
 }; 
 wp_button_pointer_array[2] = { 
     'element' : 'some-element-id', 
     'options' : { 
         'content': 'The HTML content to show inside the pointer', 
         'position': {'edge': 'top', 'align': 'center'} 
     }
};
4
  • 1
    on a JavaScript technical note, new array allocation uses ... = [];, not ... = new Array();, and in this case should just be wp_button_pointer_array = [ {...}, {...} ]; since JavaScript's basic syntax allows for arrays with content. First making an empty array and then filling it is usually discouraged unless the filling happens in dynamic code. Commented Aug 20, 2014 at 19:02
  • 1
    You are also skipping the index 0. Commented Aug 20, 2014 at 19:05
  • 1
    @Mike'Pomax'Kamermans new Array() can be used, and treated as a normal array. The difference is that it's using Constructor notation VS literal notation, so Array Object other than an Array. Commented Aug 20, 2014 at 19:06
  • 1
    while it can be because the spec allows for it, the language has dedicated syntax for array construction, and the use of new Array() is discouraged (not "bad", just discouraged - unlike new Array(size), which is usually bad unless you need to create a pre-length-assigned array that you then immediately collapse) Commented Aug 20, 2014 at 19:09

3 Answers 3

1

I'm not really sure that I understand your question, but is this what you're trying to do?

function findElementIdByName(arr, name) {
  for (var i = 0; i < arr.length; i++)
    if (arr[i].element == name)
      return i;
  return -1; //not found
}

//example call with your data
var eleId = findElementIdByName(wp_button_pointer_array, 'some-element-id');

side note: array indexing starts at 0 in javascript, and you can use new Array(), but [] is slightly faster (~80ms on average comp) due to lexical parsing in the javascript interpreter

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

Comments

0
var wp_button_pointer_array = [
    {
        'element' : 'wp-button-pointer',
        'options' : {
            'content': 'The HTML content to show inside the pointer',
            'position': {'edge': 'top', 'align': 'center'}
        }
    },
    {
        'element' : 'some-element-id',
        'options' : {
            'content': 'The HTML content to show inside the pointer',
            'position': {'edge': 'top', 'align': 'center'}
        }

    }
];


$('a').on('click', function(){
    var clickedId = $(this).attr("id");
    for(var i = 0; i < wp_button_pointer_array.length; i++ ) {
        if(wp_button_pointer_array[i].element === clickedId) {
            //do staff with wp_button_pointer_array[i]
        }
    }
});

Comments

0

This will return the index that contains the given element property.

function getIndexByElementId(elementArray, elementId) {
    var i, len;
    for(i = 0, len = elementArray.length; i < len; i++) {
        if(elementArray[i].element === elementId) {
            return i;
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.