JQuery, getting 'undefined' message when trying to access the ID of a textarea -


i'm trying update id of form element using jquery's attr() function. debugging purposes.

i seem able assign values other elements, such attr('title','test') works fine. it's when want update id.

my entire reason wanting update id, building form fields dynamically , need assign them id's.

<!doctype html> <html> <head>  <script src="http://code.jquery.com/jquery-latest.js"></script>    <script>    function addquestion(){    $('#question0container').clone().appendto('#questionarea');    $('#add').attr('id','newid');    alerttext = $('#add').attr('id');     alert("the id is:" + alerttext);    } </script> </head> <body>  <div id="questionarea">   <div id="question0container">   <textarea id="question0" name="question0"></textarea><p>answers</p>   </div> </div>  <a href="#" onclick="addquestion();" id="add" title="add question">add question</a>  </body> </html> 

you're changing id, selector still uses old id. so, no element found. change this:

alerttext = $('#add').attr('id'); 

to this:

alerttext = $('#newid').attr('id'); 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -