#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171

    Accessing form element failure


    Hi;

    There are multiple forms in the page.

    I am trying to access form element and this part document.message_id.reply.value, is causing error! Form name and form id are identified by "message_id".



    Why do I get
    document.message_id is undefined
    javascript Code:
     function reply_message_function(message_id, sender_id, receiver_id)
    {
    $.post('http://test.goldcoast-flatmates.com/reply_message/index/'+message_id+'/'+sender_id+'/'+receiver_id, { message: document.message_id.reply.value, csrf_GCFEL: '3ff3241832b491c5fd1df97211f7977f'},function(output){$('#reply_message_div_'+message_id).html(output  ).show();});
    }
    html Code:
    <div id = "reply_message_div_53693">
    <form id = "53693" name="53693">
    <textarea class="form-control" rows="3" id ="reply" name = "reply"></textarea>
    <br />
    <button type="button" class="btn btn-default btn-xs pull-right" onclick="reply_message_function('53693','93','3782')">Send Repy</button>
    </form>
    </div>
     
     
     
     
    <!--SECOND FORM--> 
     <div id = "reply_message_div_52484">
    <form id = "52484" name="52484">
    <textarea class="form-control" rows="3" id ="reply" name = "reply"></textarea>
    <br />
    <button type="button" class="btn btn-default btn-xs pull-right" onclick="reply_message_function('52484','4112','3782')">Send Repy</button>
    </form>
    </div>
    I have also tried this but no luck!
    javascript Code:
     document.forms['message_id'].elements['reply'].value



    (Jacques1, I am using html_escape...)
    Last edited by English Breakfast Tea; December 28th, 2013 at 02:17 AM.
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    Code:
    document.getElementById(message_id).reply.value
    Since you want the value of the message_id variable, you should not be quoting it. Use getElementById since you have set id attributes on the form elements.

    Note also that ID's must be unique within the page. If both of those forms are on the same page, then having id="reply" on both textareas is invalid. Either remove the id attribute or make it unique.


    As for why you got the undefined error, you're code document.message_id.reply.value was looking for an element where the ID was literally id="message_id". If you want to use a variable in place of an object property, you have to use bracket syntax, as in:
    Code:
    document[message_id].reply.value
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171
    Originally Posted by kicken
    As for why you got the undefined error, you're code document.message_id.reply.value was looking for an element where the ID was literally id="message_id". If you want to use a variable in place of an object property, you have to use bracket syntax, as in:
    Code:
    document[message_id].reply.value
    Hi this works great. I looked for documentation for this but couldn't find. How did you get this? Would you please send me the url to ref page?



    Originally Posted by kicken
    Note also that ID's must be unique within the page. If both of those forms are on the same page, then having id="reply" on both textareas is invalid. Either remove the id attribute or make it unique.
    As simple as it looks, this thing got me a bit confused on names and ids.

    The way it always works for me is that "name"s are used for accessing POSTed data on server side and "id"s are used for Javascript accessing elements.

    But here it seems like Javascript is working with names. Can you give me a few tips on that please?

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo