0

I am very beginner to HTML and javascript, so looking for some help. I have a form and it has a submit button. When i click on the submit button the name of the button will change and it will do some action. when i click on the button again i have to do different actuion. Here is my code.

HTML:

        <form name="myform" method="post" onsubmit="return false">

        <input type="submit" id="reschedule" value="reschedule" onclick="changeContent()"/>

Javascript is :

        function changeContent(ref){
        alert(id)
        document.getElementById("reschedule").value="Submit"
        var editables = document.getElementsByClassName('editableText');
        for (var i = 1; i < editables.length; i++) {
            var newstate = !editables[i].isContentEditable;
            editables[i].contentEditable = newstate;
            editables[i].bgcolor='#FF0000'
        }
        if (document.getElementById("Submit").click) {
            alert("submit")
        }
    }

So when i click on reschedule the button name will change to submit. Now when i need to click on submit function i need to do some action. How can i do this.

3
  • when you use submit button the page will get refreshed and it wont meet your requirement. try replacing with button Commented Feb 12, 2013 at 7:11
  • Put an if condition in your function. Commented Feb 12, 2013 at 7:14
  • i think you can use click count and on the basis of that use if condition. Commented Feb 12, 2013 at 7:22

4 Answers 4

1

you should return false; at the end of function so that your page will not refresh and you can do any thing on same page. use following javascript code

        function changeContent(ref){
        alert(id)
        document.getElementById("reschedule").value="Submit"
        var editables = document.getElementsByClassName('editableText');
        for (var i = 1; i < editables.length; i++) {
            var newstate = !editables[i].isContentEditable;
            editables[i].contentEditable = newstate;
            editables[i].bgcolor='#FF0000'
        }
        if (document.getElementById("Submit").click) {
            alert("submit")
        }
        return false;
    } 
Sign up to request clarification or add additional context in comments.

Comments

0

The first solution that comes to my mind is checking the name of the button and do some action depending on that value. For example:

function changeContent(ref){
    if(document.getElementById("reschedule").value == "reschedule") {
        alert(id)
        document.getElementById("reschedule").value="Submit"
        var editables = document.getElementsByClassName('editableText');
        for (var i = 1; i < editables.length; i++) {
            var newstate = !editables[i].isContentEditable;
            editables[i].contentEditable = newstate;
            editables[i].bgcolor='#FF0000'
        }
    }
    else if(document.getElementById("reschedule").value == "Submit") {
        // do something else
    }
}

Is this what you are looking for?

2 Comments

yes i changed to button and it is working perfect.. Thanks for the help
I am using the button. but How i will submit the form if am using button. i have to change the document.form.action based on the button clicked. i know its little tricky but...
0

should be :

function changeContent()
{
    document.getElementById("reschedule").value = "Submit";
    var editables = document.getElementsByClassName('editableText');
    for (var i = 1; i < editables.length; i++) 
    {
        var newstate = !editables[i].isContentEditable;
        editables[i].contentEditable = newstate;
        editables[i].bgcolor='#FF0000'
    }
    if (document.getElementById("reschedule").value = "Submit") 
    {
       document.getElementById("reschedule").onclick=function(){
            alert('Hello this submit button click event');
       };
    }
}

1 Comment

I believe you wanted to make a comparison in the if statement, not an assignment.
0

Try to use buttons, once you submitted by clicking the submit button, then the page will be refreshed along with your action try to use button

update your button

 <input type="button" id="reschedule" value="reschedule" onclick="changeContent()"/>

then change your java script function like this

  function changeContent(ref){
        alert(id)
        document.getElementById("reschedule").value="Submit"
        var editables = document.getElementsByClassName('editableText');
        for (var i = 1; i < editables.length; i++) {
            var newstate = !editables[i].isContentEditable;
            editables[i].contentEditable = newstate;
            editables[i].bgcolor='#FF0000'
        }

        if (document.getElementById("Submit").click) {
            alert("submit");
             return true;
        }
        return false;
    } 

Update Setion :

You can call your action using Button like this in javascript function:

 document.your_form_name.action = "your_action_name";
 document.your_form_name.submit();   

1 Comment

I am using the button. but How i will submit the form if am using button. i have to change the document.form.action based on the button clicked. i know its little tricky but...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.