admin管理员组

文章数量:1026989

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");

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");
Share Improve this question edited Mar 11, 2013 at 14:46 minimal asked Mar 11, 2013 at 14:16 minimalminimal 4573 silver badges15 bronze badges 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). – T.J. Crowder Commented Mar 11, 2013 at 14:18
  • This is how you use array $('#'+array[0]).css("left"); – hop 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: – HMR Commented Mar 11, 2013 at 14:28
Add a ment  | 

4 Answers 4

Reset to default 4

Taking a wild swing at it (see my ment 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 es 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.

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

I think you are looking for this :

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

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);

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");

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");
Share Improve this question edited Mar 11, 2013 at 14:46 minimal asked Mar 11, 2013 at 14:16 minimalminimal 4573 silver badges15 bronze badges 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). – T.J. Crowder Commented Mar 11, 2013 at 14:18
  • This is how you use array $('#'+array[0]).css("left"); – hop 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: – HMR Commented Mar 11, 2013 at 14:28
Add a ment  | 

4 Answers 4

Reset to default 4

Taking a wild swing at it (see my ment 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 es 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.

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

I think you are looking for this :

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

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);

本文标签: javascriptjQuery getting value from dynamic arrayStack Overflow