ga-wdi-exercises / project1

[project] GA- Project 1
3 stars 75 forks source link

dynamically generated jQuery selectors #125

Closed tomBeach closed 9 years ago

tomBeach commented 9 years ago

I am trying to select DOM elements via jQuery with dynamically generated strings that correspond to the id of the target element. It's not working (can't seem to find the element... returns undefined)!

var chipIdString = 'chipsBalp'; chipIdString = chipIdString + nextPlayer.playerIndex + ''; // nextPlayer.playerIndex from playerObject nextChipCount1El = $("#columnMiddle").find("#" + chipIdString + '_1').eq(i + 1);

andrewsunglaekim commented 9 years ago

i would console.log(chipIdString) to see what the return value of that variable is. Than see if i have a corresponding html element within the $("#columnMiddle") element after you reassign it on the second line

adambray commented 9 years ago

Hi Tom,

Few things to try:

  1. If you're finding by ID, there should only be one item on the page with that ID, thus, you shouldn't need to scope it with .find. That is to say, something like: $("#" + chipIdString + '_1') should work just as well.
  2. As Andy suggested, can you console.log(chipIdString) and make sure it's what you intend, and then manually run$("#" + chipIdString + '_1')` in the inspector console and see what you get?
adambray commented 9 years ago

@tomBeach Closing this out, but feel free to re-open and comment if you still need help.