0

I know this topic is asked before but they were not about jqueryui. Therefore I couldn't be sure will they work on my code. I have a pop up that is called from different components. But I have to write a click function for each call. I want to convert it to ca function that I can call it. My script is:

 <script type="text/javascript">
        $(document).ready(function () {
            $('a#popup').live('click', function (e) {

                var page = $(this).attr("href")

                var $dialog = $('<div id="silmeIframe"></div>')
                .html('<iframe  style="border: 0px; " src="' + page + '" width="500px" height="500px"></iframe>')
                .dialog({
                    autoOpen: false,
                    modal: true,
                    height: 'auto',
                    resizable: 'false',
                    width: 'auto',
                    title: "Silmeyi onaylıyor musunuz?",
                    close: function (event, ui) {

                        __doPostBack('<%= btnRefresh.ClientID %>', '');
                    }
                });
                $dialog.dialog('open');
                e.preventDefault();

            });

        });
    </script>

Now, I want it work like

<a onclick="NewFunctionName(parameter1,parameter2,,parameter3)">click<a/>
3
  • i don't get what's wrong with this one. why do you create handlers for each? jQuery can attach a single handler to multiple elements. Commented Apr 24, 2012 at 0:56
  • @Joseph there is nothing wrong, he just want to transform it Commented Apr 24, 2012 at 0:59
  • yes, I only want to transform it. Commented Apr 24, 2012 at 1:00

1 Answer 1

1

single handler, multiple targets attachment

$(function () {

    function handler(e,param1,param2...) {
        var page = $(this).attr("href")
        ...
        $dialog.dialog('open');
        e.preventDefault();
    }

    //write attachments
    $('a#popup1').on('click',function(e){
        handler(e,param1,param2...);
    });
    $('a#popup2').on('click',function(e){
        handler(e,param1,param2...);
    });
    $('a#popup3').on('click',function(e){
        handler(e,param1,param2...);
    });
});

or single handler, multiple targets in one query

$(function () {
    $('a#popup1, a#popup2, a#popup3').on('click',function (e) {
        var page = $(this).attr("href")
        ...
        $dialog.dialog('open');
        e.preventDefault();
    });
});
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your answer. I have tried it. But I dont know how use handler in the first method. Do I have to write just like you wirte. Or inside the <a> tag?
@user1352563 just the way it is. you target the <a> when you attach (the parts with a#popup). you should avoid inline JavaScript in elements.
I have wrote it like that but it doesn't work. It opens the <a> tag href link on a new page. But it should be opened as a pop up. That is , I think, it does not see the popup id.
@user1352563 did you include an e.preventDefault(); or return false at the end of your handler code? what does the console say? did you target the <a> properly?
I have deleted }); at last part because it gives syntax error. After that I tried putting document ready at the top of the script. Both of them did not work. And then I tried by deleting e.preventDefault();. there is no return false already.
|

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.