最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery getting value from dynamic array - Stack Overflow

matteradmin5PV0评论

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);
Post a comment

comment list (0)

  1. No comments so far