SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript not printing input Data

    Hello All,
    I am trying to get the data what user has given in 4 TextBox.But i could not get the data from javascript end.Can anyone please check and tell me the correct way( yes i wants to use for loop and the control name create dynamically).
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="javascript">
    function mfData()
    {
    var strData="";
    for(var i=1;i<5;i++)
    {
    var pos='txt'+i;
    if(eval('document.frm.'+pos+'.value')=="")
    	{
    	alert ("data not found"+pos);
    	return false;
    	}
    	else
    	{
        strData=strData+eval('document.frm.'+pos+'.value');
        }
    }
    alert (strData);
    return true;
    }
    </script>
    
    </head>
    
    <body > 
    <form name="frm">
    <table>
    <tr><td>User 1</td><td><textarea id="txt1" name="txt1"></textarea></td></tr>
    <tr><td>User 2</td><td><textarea id="txt2" name="txt2"></textarea></td></tr>
    <tr><td>User 3</td><td><textarea id="txt3" name="txt3"></textarea></td></tr>
    <tr><td>User 4</td><td><textarea id="txt4" name="txt4"></textarea></td></tr>
    <tr><td colspan="2"><input type="button" value="Click" name="nbtn" onclick="javascript:return mfData();" /></td></tr>
    </table>
    </form>
    </body>
    </html>
    -::Ujjwal::-
    Visit My Site

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You can start by replacing

    eval('document.frm.'+pos+'.value')

    with

    document.getElementById(pos).value
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now i have changed it to dom Object Model
    The code is as follows:
    Code:
    function validateAllFields(objForm)
    	{		
    		      var count=document.frm.hdnCount.value;   
            	  var data="";
    			  var checkStatus;
    			   for(var i=1; i<=count; i++)     
        				{
    					checkStatus=false;
    					var pos1='txtAnswer_'+i;
    					debugger;
    					var strAns=document.getElementById(pos1);
    					
    					var pos='hdnQuestionID_'+i;
    					var strQuest=document.getElementById(pos);
    					
    					if(strAns.Value!="")
    						{
    						data=data+"~"+strQuest.value+" $"+strAns.value;
    						checkStatus=true;
    						}
    					
    						if(!checkStatus)
    						{
    						alert("Please choose anyone from this question");
    						document.getElementById(pos1).focus();
    						return false;
                            }
    					}
    					alert(data);
    					if(data!="")
    					{
    					data=data.substr(1);
    					}			
    		 document.frm.hdnQuestionAnswerData.value=data;      
             document.frm.mode.value='WARMUP';     
               return true;
    		
    		}
    -::Ujjwal::-
    Visit My Site


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •