views:

31

answers:

2

I have button on my HTML page. Initially i was using

<input name="op" id="changepass_submit" value="Change Password" disabled="disabled" class="form-submit" type="button" onClick="change_passwd();" />. 

Using this i am able to call the javascript function change_passwd() which has it's definition as,

function change_passwd()
{
    var o_pass = document.getElementById('o_passwd').value;
    var n_pass = document.getElementById('n_passwd').value;
    var c_pass = document.getElementById('c_passwd').value;
    if(n_pass == "")
        alert("New Password can not be empty!");
    else 
    {
        if(n_pass == o_pass)
        {
            alert('New password and old password are same! Please choose different new password.');
        }
        else
        {
            o_pass = rtrim(ltrim(o_pass));
            n_pass = rtrim(ltrim(n_pass));
            $.ajax({
                type: "POST",
                data: {o_pass:o_pass,n_pass:n_pass},
                url: "changepass",
                success: function(response) {
                    var result = eval(response);
                    alert(result[1]);
                    window.location = "/my_profile";
                }
            });
        }
    }
}

It works fine.

But when i change <input> tag to:

<button name="op" id="changepass_submit" disabled="disabled" class="form-submit" onClick="change_passwd();" style="width:150px;"><strong>Change Password</strong></button>

it gives jQuery error..

why so? If any body has any suggestions or answers pls let me know. Thanks in advance.

A: 

What exact error do you get?

What line in your code is triggering this error?

Shadow Wizard
Sorry! Please see the updated question.
Aashish P
I still need the full error message to have chance of solving it.. :)
Shadow Wizard
I have seen error in Firebug console as error on line 13 in jQuery.js file.
Aashish P
I really, really doubt that the error has anything to do with changing the `<input>` tag like that.
Pointy
bobince found the problem.. just add type="button" in the button tag and it will work fine.
Shadow Wizard
+2  A: 

I don't know about the jQuery error (what's the error?) but a <button> is type="submit" by default and not type="button". That means when clicked (assuming it is not disabled as in the markup), since you do not return false to cancel the default action, the form will continue to submit, potentially cancelling your ajax() operation.

bobince
He's got `type="button"` way down on the far end of the opening tag.
Pointy
i don't have any form in the HTML page. Still it can give this error ?
Aashish P
It worked when i put attribute type="button" in <button> tag. ty bobince. I have one question when we are specifying that the tag is button why to give type="button" ??
Aashish P
@bobince **oh** now I see - he went from `<input>` to `<button>`. Boy I'm pretty slow sometimes.
Pointy
`<button>` can replace three types of button: actionless `button`, a form `submit` or a `reset`. So it's necessary to state which you mean. I guess `submit` was chosen because `button` on its own does nothing, requiring JavaScript intervention, so `submit` was seen as the more likely case. (IE didn't agree, defaulting to `button` instead, so omitting `type` is unreliable.) Incidentally you don't necessarily need to use a `<button>` if all you want is bold text; a plain `input` with CSS `font-weight` can do that.
bobince
oh.. pretty useful information. Thanks a lot. :-)
Aashish P