To run every time with .click ()?

I tried to have .click()

in <a>

to find out that it won't start every time I click, what it should do is open a dialog.

This is not the only problem I need to pass a value to my jquery. I just can't figure it out.

I need it to be there <a>

because it disappeared in the dropdown. Do you have any suggestions?

EDIT this is the code i am using and it works

$(document).ready(function() {
        $('#dialog').dialog({ autoOpen: false, bgiframe: true, modal: true, height: 600, width: 1000, Close: function() { $(this).dialog('destroy'); } });

        $('a').live('click', function(evt) {
            evt.preventDefault();
            var ids = $(this).attr('id');
            var first = "<iframe style='width: 100%; height: 100%;' src='" + "" + "'</iframe>'";
            $('.iframe').html(ids);

            $('#dialog').dialog('open');
        });
    });

      

This code sets the dialog to open on every click and it works every time, the trick for me here is "destroy" at the end and nudge

+2


a source to share


3 answers


This will fire on every click. While I doubt this will be the case, make sure you don't have any other event handlers listening on "a" that apply event.stopImmediatePropagation()

. Try adding return false

to the end of your click handler, or even better:

$('a').click(function(evt) {
        var first = "<iframe style='width: 100%; height: 100%;' src='" + need to put value here + "'</iframe>'";
        $('.iframe').html(first);
        $('#dialog').dialog({ bgiframe: true, modal: true, height: 600, width: 1000 });

        evt.preventDefault();
    });

      

You might find that the page is reloading if you don't prevent the anchor tag's default action, which would give the impression that nothing is happening.

What "value" do you think? You can use jQuery data () to store information, and of course you have access to all the global variables in that scope.

EDIT: To respond to your comment, you can get the id a inside the click event like this:



var theIdOfTheA = $(this).attr('id');

      

Note that this must be placed inside a handler.

EDIT2:

$('a').live('click', function(evt) {
        var first = "<iframe style='width: 100%; height: 100%;' src='" + $(this).attr('id') + "'</iframe>'";
        $('.iframe').html(first);
        $('#dialog').dialog({ bgiframe: true, modal: true, height: 600, width: 1000 });

        evt.preventDefault();
    });

      

+1


a source


DEMO: http://jsbin.com/olalu/edit

make sure you close the dialog before opening it again!

<a href='javascript:;' id='my_unique_id' >click</a>

      



EDITED

$('a').click(function(evt) {
    evt.preventDefault();

    var theIdOfTheA = this.id;

    var first = "<iframe style='width: 100%; height: 100%;' src='" + 
                "http://www.sf.se" + "'</iframe>'";
      $('.iframe').html(theIdOfTheA);
       $('#dialog').dialog({  
       bgiframe: true, 
       modal: true, 
       height: 600, 
       width: 1000, 
       //this
       Close: function() { $(this).dialog('close'); },
       //OR this OR both
       Cancel: function() { $(this).dialog('close'); }
    });
 });

      

+1


a source


You can pass data to your event handler by doing the following (from the jQuery docs ):

$('#foo').bind('custom', function(event, param1, param2) {
  alert(param1 + "\n" + param2);
});
$('#foo').trigger('custom', ['Custom', 'Event']);

      

So, you can do this:

$(document).ready(function() {
    $('a').bind("click", function(event, myValue) {
        var first = "<iframe style='width: 100%; height: 100%;' src='" + myValue + "'</iframe>'";
        $('.iframe').html(first);
        $('#dialog').dialog({ bgiframe: true, modal: true, height: 600, width: 1000 });
    });
});

      

Note that you are now using bind () to bind the event handler and the function gets the value (call it whatever you want). To show google in an iframe:

$('a').trigger('click', ['http://www.google.com']);

      

0


a source







All Articles