Coding Duchess
Coding Duchess

Reputation: 6909

Converting IE specific javascript with "for" attribute

I have the a form on my page and a script to go with it that works in IE. I need to get it working in Chrome. My code is below

<script for="Form1" event="onsubmit()" language="JScript">
        if (this.text1.value == '') 
        {
            alert ("Please enter the value")
            this.text1.focus()
            return false
        }

        this.hiddentext1.value = this.text1.value;

        // Disable button
        with ($('button1')) {
            value = "Processing ...";
            disabled = true;
        }


        return true;        
    </script>

The form is as follows:

    <form method="post" name="Form1" action="default.asp">

        <input type="hidden" name="hiddentext1">


        <input type="text" name="text1">
        <input type="submit" value="Submit" id="button1">
    </form>

it works fine, submitting the form. But I also need to get it to work in Chrome, so I have to get rid of

<script type="text/jscript">
function ProcessField()
{
     alert("script gets called");
     alert("input value is " & this.text1.value);
        if (this.text1.value == '') 
        {
            alert ("Please enter the value")
            this.text1.focus()
            return false
        }

        this.hiddentext1.value = this.text1.value;

        // Disable button
        with ($('button1')) {
            value = "Processing ...";
            disabled = true;
        }


        return true;    
}   
    </script>

The form is as follows:

    <form method="post" name="Form1" action="default.asp" onsubmit="return processField();">

        <input type="hidden" name="hiddentext1">


        <input type="text" name="text1">
        <input type="submit" value="Submit" id="button1">
    </form>

The script gets called but apparently crashes - "script gets called" part gets displayed but second alert does not fire. What am I doing wrong here?

Upvotes: 0

Views: 29

Answers (1)

epascarello
epascarello

Reputation: 207501

Looks like you are mixing up syntax for string concatenation with another language.

alert("input value is " & this.text1.value); 

should be

alert("input value is " + this.text1.value);

and you probably will have an issue with disabling the button before the form submits. And this is the window and not the form.

function processField(form)  //changed to lowercase p, pass in form
{  
     alert("script gets called");
     alert("input value is " + form.text1.value);  //changed & to +, updated this to form
        if (form.text1.value == '') //updated this to form
        {
            alert ("Please enter the value")
            form.text1.focus()  //updated form
            return false
        }

        form.hiddentext1.value = form.text1.value;  //updated this to form

        // Disable button
        with ($('button1')) {
            value = "Processing ...";
            disabled = true;
        }


        return true;    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" name="Form1" action="default.asp" onsubmit="return processField(this);">  <!-- pass in this -->

        <input type="hidden" name="hiddentext1">


        <input type="text" name="text1">
        <input type="submit" value="Submit" id="button1">
    </form>

Upvotes: 1

Related Questions