Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

html - Function within Function in Javascript not working

I have submit function on a button. Upon clicking "submit" a dialog box pops and asks for confirmation and after clicking "ok" the form is submitted.

Now I want to add another function in which upon clicking "ok" the form should be submitted & background color of page should also change. The code I wrote is given below. but now even the form is not submitting nor background color is changing. Help needed!

function myfunction(e) {
        if (!confirm('Are you sure to proceed with this role?')) {
            changecolor(e); 
            e.preventDefault();
        } else {
            return false;
        }
    }

    function changecolor(e) { 
        e.target.classList.add('custom_back')
    }

.custom_back{
backgroundcolor: #0065bd; }
question from:https://stackoverflow.com/questions/65870432/function-within-function-in-javascript-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

code is fine function within a function will always work you just need to remove not condition in if.

here is working code

function myfunction(e) {
        if (confirm('Are you sure to proceed with this role?')) {
            changecolor(e); 
            e.preventDefault();
        } else {
            return false;
        }
    }

    function changecolor(e) { 
        e.target.classList.add('custom_back');
    }
.custom_back{
background:green;
color:white
}
<input type="submit" onclick=myfunction(event) value="Submit"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...