views:

19

answers:

1

Hi, I have been trying to solve this for a while. I am trying to get value of a form input which is inside a nested loop. But its coming undefined. Any idea why so? Thanks much for your help in advance. Here is the code for example.

I am getting the value of $(#fund-no-"+f).attr('value') without any problem. Its $("#year-" + year_amt + "-" + f).attr('value') that i cant retrieve. It comes undefined. If i use $("#year-" + year_amt + "-" + f).val() instead i dont get any value at all.

 var fund_datastring;
 fund_datastring = "";
 if(fundqty >0)
 { 
  for(var f =1; f <= fundqty; f++)
  {
   fund_datastring += "&fund_no_" + f + "=" + $("#fund-no-" + f).attr('value') ;  

     var fundyear_datastring = "";
     var hidden_fundid = $("#hidden-fund-id-"+f).attr('value');

   var year_amt=2;
   //for(var year_amt =2; year_amt < hidden_fundid; year_amt++)
   while (year_amt < hidden_fundid)
   {
    fundyear_datastring += "&hidden_fund_"+f+ "=" + hidden_fundid + "&fund_"+ f +"_amount_"+ year_amt + "=" + $("#year-" + year_amt + "-" + f).attr('value') + "&fund_"+ f +"_year_"+ year_amt + "=" + year_amt;

   year_amt++
   }
   hidden_fundid = "";


  }


 }


<div class="fsFieldHorizontal"> 
    <input type="text" value="" class="fsField" size="32" name="year_1_1" id="year-1-1"> 
    <input type="hidden" value="4" id="hidden-fund-id-1"> 
        <a onclick="addFundForm(this); return false;" id="add-year-1" href="#"> 
            <img alt="Add Year" src="images/add_16.png"> 
        </a><br><br> 
        <div id="year-container-1"> 
            <div id="year-2-1" style="margin: 5px 0px;">
                <input type="text" value="" class="fsField" size="32" name="year_2_1" id="year-2-1">
                    <a onclick="removeFundYear("#year-2-1"); return false;" href="#">
                        <img alt="Remove Year 2" src="images/cancel_16.png">
                    </a><br>
                    <label for="year-2-1" class="fsSupporting">Year 2 Amount</label>
            </div>
            <div id="year-3-1" style="margin: 5px 0px;">
                <input type="text" value="" class="fsField" size="32" name="year_3_1" id="year-3-1">
                    <a onclick="removeFundYear("#year-3-1"); return false;" href="#">
                        <img alt="Remove Year 3" src="images/cancel_16.png">
                    </a><br>
                    <label for="year-3-1" class="fsSupporting">Year 3 Amount</label>
            </div>
        </div> 
</div>
+3  A: 
T.J. Crowder
Thanks. I am doing it inside a jQuery funciton. Its my mistake. I meant .val(). I dont get any value even with $("#year-" + year_amt + "-" + f).val(). I checked it in Firebug where there is no value recorded.
Abu
@Abu: I misread the `val` part of your question originally, sorry. Updated.
T.J. Crowder
If there are multiple elements with the same id (which is bad practice) jQuery just return the first one, unless you do `$('[id=myId]')` which fulls it, but looses the performance gain of the native implementation.
Dan Manastireanu
Thanks Dan. I am using unique IDs for sure. As i am creating using loops so each have a different ID. I attached the HTML that was seen in the Firebug window.
Abu
@Dan: If jQuery's Sizzle engine is deferring to `document.getElementById` in the simple ID selector case (and I believe it is), you **cannot** rely on it matching the first such element in the tree. The behavior can vary amongst implementations. Some implementations may return the last, rather than the first, for instance.
T.J. Crowder
@T.J. Crowder: True, it uses the native implementation of the browser, but so far I haven't seen a browser that behaves differently ... although there might be
Dan Manastireanu
@Abu - DIV with ID `year-2-1` is on line 8 **and** 9 of the HTML. It's not unique. ==> `<div id="year-2-1" style="margin: 5px 0px;"><input type="text" value="" class="fsField" size="32" name="year_2_1" id="year-2-1">`
Peter Ajtai
@Abu: You have a `div` and an `input` with the same ID. Updated my answer.
T.J. Crowder
@Peter: Jinx! ;)
T.J. Crowder
Thanks T.J. I havent seen that out of so many ids...let me try it after i change.
Abu
Huge Thanks to T.J. Great response. I didn't even think of checking the Div ids. I changed teh Div id's to "divyear-#-#" and it works great. I was more looking into the source instead of the HTML code which is why the error escaped. Will be more careful in the future. Thanks a lot everyone for your answers. Great Site!
Abu
@Abu: Good deal, glad that helped! Yeah, it's a good place -- stick around, answer a few questions (but try not to get addicted and neglect your *own* work -- it's been known to happen). :-)
T.J. Crowder