简体   繁体   中英

Define ID of clicked element and pass it through to another function

I want to pass variable "pageSetUp" from the on click event go through to function x and I keep going in circles.. I'm still a bit of noob when it comes to js. I'd really appreciate your help. Thanks!

$(".link").click(function() {
    var pageSetUp = $(this).attr("ID");  
});

function x(pageSetUp){
        if(pageSetUp == page1){             
            //do something
        }
        else if(pageSetUp == page2){
            //do something else
        }else{

        }                   
});

x();

you need call x with desired parameter.:

$(".link").click(function() {
    var pageSetUp = $(this).attr("ID");
    x(pageSetUp);
});

function x(pageSetUp){
        if(pageSetUp == page1){             
            //do something
        }
        else if(pageSetUp == page2){
            //do something else
        }else{

        }                   
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM