Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 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 Reply

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"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...