954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

getting the text field value

how do I get The value I have entered in a textfield after submitting it. i need to process it using java

thanks

Covinus
Junior Poster
112 posts since Mar 2006
Reputation Points: 31
Solved Threads: 0
 

Get the value AFTER you submit it? That would be a server-side question. Since you mention Java, I suggest you ask in the Java forum. All values are packaged in the RESPONSE object, but that object is exposed in different ways in different languages.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

opps i i want to get it using javascript.

hehehe

Covinus
Junior Poster
112 posts since Mar 2006
Reputation Points: 31
Solved Threads: 0
 

[HTML][/HTML]

That's how to reference a control in an HTML form, but after you submit? I don't get it. More info required.

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

If the text element has an ID, as it should it XHTML, the DOM method is:

document.getElementById['myId'].value

I'm not sure what you're hinting at by "after the submit", though, in your question.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

That won't work with square brackets, but with parentheses that is fine.

Both our methods are the best cross browser methods. As long as you avoid document.all or document.txtFieldName you should be ok.

I'm not sure what you're hinting at by "after the submit", though, in your question.



Well Covinus said:how do I get The value I have entered in a textfield after submitting

And I'm trying to think of why would you want to reference a control in client side script after a submit? perhaps the submit is irrelevant in this case and a red herring just to keep us on our toes!

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

here is what i remember about my code.

var a = document.formname.textname.value generates an error mesasge

everytime i add this to my code nothing appears.
but when i remove the other stuff comes out.

Covinus
Junior Poster
112 posts since Mar 2006
Reputation Points: 31
Solved Threads: 0
 

do you really need the semicolon in last line??? i found books that dont have ; in the end

Covinus
Junior Poster
112 posts since Mar 2006
Reputation Points: 31
Solved Threads: 0
 

JavaScript uses the semi-colon for line-termination. For the proper method(s) of placing the value of a textbox into a variable, please refer to the posts from myself and hollystyles in this thread.

If you have new, different questions about JavaScript, please start a new thread.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 
do you really need the semicolon in last line??? i found books that dont have ; in the end

It is true some browsers will run javascript without ; line terminators. But it is bad practice because the ECMA standard states ; is required and browser implimentations could change in the future. So any script you write without them, may work now but could be rendered useless after a browser upgrade.

I would say if you have books that omit ; they are out-of-date or just poor. If you can get hold of it in the Philippines I recommend the Javascript Anthology by James Edwards and Cameron Adams published by Sitepoint ISBN 0-9752402-6-9

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 
<html>
<head>
<title>
hi!!!
</title>
<script type="text/javascript">


function operator(op){
if(op==0)
return "+"
if(op==1)
return "-"
if(op==2)
return "x"
else
return "/"
}
    function add(n1, n2, n3, n4){
    var ans
    if(n3==0){
    ans=n1+n2
    if(ans ==n4)
    alert("you got it right!")
    else
    alert("Sorry its wrong. the answer is: "+ans)
    }
    if(n3==1){
    ans=n1-n2
    if(ans==n4)
    alert("you got it right!")
    else
    alert("Sorry its wrong. the answer is: "+ans)
    }
    if(n3==2){
    ans=n1*n2
    if(ans ==n4)
    alert("you got it right!")
    else
    alert("Sorry its wrong. the answer is: "+ans)
    }
    if(n3==3){
    ans=n1/n2
    if(ans ==n4)
    alert("you got it right!")
    else
    alert("Sorry its wrong. the answer is: "+ans)
    }
    }
    
    
    
</script>
</head>
    <body>
    <font color="black" size="7"><center>
    <script type="text/javascript">
    
    
    var num1=(Math.floor(Math.random()*11))
    
    var num2=(Math.floor(Math.random()*11))
    
    var operation=(Math.floor(Math.random()*3))
    
    var convertedOp=operator(operation)
    
    document.write(num1 + "" + convertedOp+ " " + num2 + "")
    document.write("______")
    var answer=document.form1.tfield.value;
    
    
    function check(){
    
    add(num1,num2,operation,answer)
    }
    </script>
    <form action="test.html" name="pormas">
    <input type="text" size=3 name="tfield" id="ans" value=0>
    <input type="button" value="Answer" name="ans" onclick="return check()">
    </form>
    
</body>
</html>

this is my code for generating a number and do an operation randomly too. but i cant get the user input so i cant process it. it seems that the answer variable is always undefined.

Covinus
Junior Poster
112 posts since Mar 2006
Reputation Points: 31
Solved Threads: 0
 

we both have the same problem..getting the data from the text field inorder for us to compare it and generate it in a function..

jaepi
Practically a Master Poster
647 posts since Jul 2006
Reputation Points: 32
Solved Threads: 4
 

Number one READ what we are telling you.
1. PUT ; at the end of your statements.
2. replace var answer=document.form1.tfield.value; with what we told you THREE times now var answer=document.getElementById['tfield'].value

Now this line var answer=document.form1.tfield.value; is not only syntacticly wrong, it's in the wrong place. It will get parsed and executed by the browser BEFORE the text box "tfield" exists in the DOM (Document Object Model) so instead you need to move it into function Check()

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

And you have this bit wrong

<input type="text" size=3 name="tfield" id="ans" value=0> it should be <input type="text" size=3 name="tfield" id="tfield" value=0>

You can't reference the textbox by name, you must use the ID.

this code works

<html>
<head>
<title>
hi!!!
</title>
<script type="text/javascript">


function operator(op){
if(op==0)
return "+";
if(op==1)
return "-";
if(op==2)
return "x";
else
return "/";
}
function add(n1, n2, n3, n4){
var ans;
if(n3==0){
ans=n1+n2;
if(ans ==n4)
alert("you got it right!");
else
alert("Sorry its wrong. the answer is: "+ans);
}
if(n3==1){
ans=n1-n2;
if(ans==n4)
alert("you got it right!") ;
else
alert("Sorry its wrong. the answer is: "+ans);
}
if(n3==2){
ans=n1*n2 ;
if(ans ==n4)
alert("you got it right!")
else
alert("Sorry its wrong. the answer is: "+ans) ;
}
if(n3==3){
ans=n1/n2  ;
if(ans ==n4)
alert("you got it right!") ;
else
alert("Sorry its wrong. the answer is: "+ans);
}
}



</script>
</head>
<body>
<font color="black" size="7"><center>
<script type="text/javascript">


var num1=(Math.floor(Math.random()*11));

var num2=(Math.floor(Math.random()*11));

var operation=(Math.floor(Math.random()*3));

var convertedOp=operator(operation);

document.write(num1 + "" + convertedOp+ " " + num2 + "");
document.write("______");



function check(){
var answer=document.getElementById("tfield").value;
add(num1,num2,operation,answer) ;
}
</script>
<form action="test.html" name="pormas">
<input type="text" size=3 name="tfield" id="tfield" value=0>
<input type="button" value="Answer" name="ans" onclick="return check()">
</form>

</body>
</html>
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

sorry..

ok i get it

Covinus
Junior Poster
112 posts since Mar 2006
Reputation Points: 31
Solved Threads: 0
 

function b1(){

var val=document.getElementsById("t1").value;

$('#i1').rotate({maxAngle:360,minAngle:0,
bind: [
{"mouseover":function(){$(this).rotateAnimation(122);}},
{"mouseout":function(){$(this).rotateAnimation('val');}}
]
});
}

i want to rotate my image using java script.i put some value in text field i.e. #t1
and call the value of the text field into the rotateAnimation function.
is this possible pl help ..........

kapil.kumar13
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You