Click here to Skip to main content
16,016,759 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

This little piece of code got a mind of its own? The sky starts with Blue and when the Pink is click, it turns to pink ok, but when submit 'test' button to trun it to Dark, the JS keep reporting an error of Null object box1.

Any one can see what I did wrong, please give a hint please.

PS. where I can find a good reference for mixing JS, ASP aqnd html?

XML
<script language="javascript" type="text/javascript">
function putText(s) {
alert(s);
    document.f.box1.value = s;
    document.f.submit();
}
</script>

<body>
<%
box1 = request("box1")
if box1 = "" then
    box1 = "The sky is blue today"
    end if

if request("test") = "Dark" then
    response.Write("<script>putText('Submit to get dark sky')</script>")
    end if
%>

<form name="f" id="f" method="post">
<input name="box1" id="box1" type="text" value="<%=box1%>" />
<input type="submit" value="Pink" onClick="putText('Click to Pink Sky')"  />
<input name="test" type="submit" value="Dark"  />
</form>
</body>


rgds,
kfl.
Posted

1 solution

The problem with your code is:
When you click the 'Dark' button, the JavaScript is called but the form and the other elements are not available at that time. So it gives an error: document.f.box1 is null or not an object.

To make this code working, move if request("test")... part to below dark button declaration.
I am giving you the working code below:

XML
<html>
<script language="javascript" type="text/javascript">
function putText(s) {
alert(s);
    document.f.box1.value = s;
    document.f.submit();
}
</script>
    <%
    box1 = request.form("box1")
    if box1 = "" then
        box1 = "The sky is blue today"
    end if
    %>
<body>
    <form name="f" id="f" method="post">
        <input name="box1" id="box1" type="text" value="<%=box1%>" >
        <input type="submit" value="Pink" onClick="putText('Click to Pink Sky')"  >
        <input name="test" id="test" type="submit" value="Dark"  >
        <%
        if request.form("test") = "Dark" then
            response.Write("<script>putText('Submit to get dark sky')</script>")
        end if
        %>
    </form>
</body>
</html>


Hope this solves the problem and clears your doubt.

[Edit]
And you don't even need to submit the form twice in the JavaScript function. So remove document.f.submit(); from the function.
[/Edit]
 
Share this answer
 
v3
Comments
KFLee 20-Nov-10 15:56pm    
Thanks, I tested it and work as said. This takes me back to the sequential programming days of Fortran/C/Pascal. Remember in Pascal where we always put Main() at the end as it works in single pass!

I still has not grasp the concept of "submit" in this case. It did not have any effect as [Edit] added. I test further to try understand the logic and now it encounters a different problem when using span. I submit it as a different question. Please check it.

rgds,
kfl.
Ankur\m/ 21-Nov-10 0:18am    
Hi, yeah even I didn't expect that. But when I ran that code with debugging enabled, I saw the code broke at the JavaScript function and when I checked the HTML source, the element was not available. So I restructured the code.

You are submitting the form again in the Javascript function which is not required. The function is anyways called when a submit (postback) takes place.
I will check the question in sometime. I just woke up sometime back and will get freshen up now.
BTW, you should accept the answer and may vote accordingly if it answers your question. ;)
KFLee 21-Nov-10 7:16am    
Hi, while thinking about this problem, it occured to me that the key difference is in Submit and Click in this example. Am I right to think that the Submit would submit the data from the browser back to server while Click would simple processed by the local browser, that would explain why it behave different. Is this correct?

rgds,
kfl.
ps. I have voted it and thanks for the answer.
Ankur\m/ 21-Nov-10 7:54am    
Yes, a submit postbacks the page i.e send it to the server whereas a onclick event would be fired and handled locally by the browser.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900