1

I have an array with divs ids (in my case its all divs ID values od parent div (#area) ):

jQuery.fn.getIdArray = function () {
 var ret = [];
 $('[id]', this).each(function () {
  ret.push(this.id);
 });
 return ret;
 };

var array = $("#area").getIdArray();

I need to get an array field value, something like this:

var lef = $("#array".[0]).css("left");
3
  • 2
    I'm afraid your question is a bit too "minimal". ;-) What is getIdArray's return value? What do you mean you need to get an array field value (the phrase is fairly clear, but then the code after it doesn't seem to relate to it). Commented Mar 11, 2013 at 14:18
  • This is how you use array $('#'+array[0]).css("left"); Commented Mar 11, 2013 at 14:19
  • We still don't know what .getIdArray() returns. If you have a custom filter you can: $("#area").filter(function(){//return true if you want this one used}).css("left") to make your custom filter you can: Commented Mar 11, 2013 at 14:28

4 Answers 4

4

Taking a wild swing at it (see my comment on the question):

var array = $("#area").getIdArray();
var lef=$("#" + array[0]).css("left");

That assumes that getIdArray returns an array of strings, where each string is an id value for a DOM element, and that you want to get the left value for the first of those elements.

So for instance, if the array comes back as:

["foo", "bar", "charlie"]

then the selector created by "#" + array[0] is #foo, so you end up getting the left value for the foo element.

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

Comments

2

If you have an actual JS array within your variable array just use bracket notation to access each individual ID.

// I have the # before-hand since I'm assuming you have just the ID name
var lef = $('#' + array[0]) // this will access the 1st one in the array

Comments

1

I think you are looking for this :

var divYouWantToChange = $("#"+array[0]);

Comments

1

I try to formulate this as an answer because getIdArray is not a jquery function and we don't know what it does. If you'd like to apply a custom filter to the $("#area") collection you can do so using filter. This will return a jquery object where you can get the .css("left") from.

If you'd like to save both the id's and the left property you can do so with the following code:

var objects=[];
$("#area").filter(function(){
  $this=$(this);//cache the object
  objects.push({id:$this.attr("id"),
   left:$this.css("left")
  };
});
console.log(objects);

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.