Forum Moderators: open
Javascript Function
-------------------
function getSorted(strOption, param, flag) {
alert("strOption: "+strOption);
alert("param: "+param);
alert("flag: "+flag);
var a = document.form.sortName.value;
if(a!=null)
{
}
else
{
alert("value is null");
}
document.form.sortName.value ="hai";
alert("document.form.sortName.value: "+document.form.sortName.value);
document.form.sortName.value = strOption;
alert("document.form.sortName.value: "+document.form.sortName.value);
if(param==1) {
document.form.sortName.value = strOption;
alert(document.form.sortName.value);
document.form.flagSet.value = flag;
document.form.method="post";
document.form.action="abc.jsp";
document.form.submit();
}
}
----------------------
I am calling this from jsp like
<img src="images/ship_name.gif" width="113" height="26" name="shipName" onClick="getSorted('shipName',1,2)" >
----------------------
The same thing works if the page where I am using this code is stand alone, but doesnt work if I include another page. I am not getting the "sortName" value at all.
What am I missing? Also I read in some forums that if I have 2 forms in the jsp page, it mght give this error and I do have one for the included page and one for the including page and both have the same form name.
If anyone wants, I can post the whole code.
Thanks
I am not getting the "sortName" value at all.
See if this gets you pointed in the right direction.
First,
var a = document.form.sortName.value;
Is your form named "form"?
<form action="whatever" name="form">
Because that's what it appears you're looking for. If that's the case, I would NOT name the form "form" name it something else, like my_form, whatever.
Second,
if(a!=null) { ... }
I'm a bit out of turn here because I don't recall the exact conditions for this - but I do know that null is not always what you think it should be. The text field may not be NULL. After you set it to the form value, it's a blank field. Try
if (a!= '') { ... }
or just
if (a) { ... }
Lastly here's a few things to play around with. The comment about two forms - this is only true if you don't reference the correct one when you call the function. A way of working with this is to not reference some objects expicitly by name, just pass the object:
<img src="images/ship_name.gif" width="113" height="26" name="shipName" onClick="getSorted(this,this.form,1,2)" >
function getSorted(strOption, form, param, flag) {
var a = strOption.value;
if(a!= '') {
alert('here is the value ' + a);
}
else
{
alert("value is not exactly null, but blank.");
}
Not that instead of passing the STRING, it's just the object representing that field by the "this" keyword. So now object.name, object.value, etc, are all available to your routine.
After recoding that - I saw that there was no reason to pass the FORM object - but you may want to do that anyway, as you may need it later
form.submit();
In this context, form is the form OBJECT. Not a name.
<form name = a>
<form name = b>
</form>
</form>
and I read a tutorial that it should not be done this way. So I changed it to
<form name = a>
</form>
<form name = b>
</form>
This worked great.
Thanks for the help once again