How do I bind Twitter Bootstrap tooltips to dynamically created elements?
Try this one: $(‘body’).tooltip({ selector: ‘[rel=tooltip]’ });
Try this one: $(‘body’).tooltip({ selector: ‘[rel=tooltip]’ });
If you are trying to trigger an event on the anchor, then the code you have will work I recreated your example in jsfiddle with an added eventHandler so you can see that it works: $(document).on(“click”, “a”, function(){ $(this).text(“It works!”); }); $(document).ready(function(){ $(“a”).trigger(“click”); }); Are you trying to cause the user to navigate to a … Read more
Andreas Grech was pretty close… it’s actually this (note the reference to this instead of the item in the loop): var $dropdown = $(“#dropdown”); $.each(result, function() { $dropdown.append($(“<option />”).val(this.ImageFolderID).text(this.Name)); });
I think the difference is nearly self-explanatory. And it’s super trivial to test. jQuery.html() treats the string as HTML, jQuery.text() treats the content as text <html> <head> <title>Test Page</title> <script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”></script> <script type=”text/javascript”> $(function(){ $(“#div1″).html(‘<a href=”https://stackoverflow.com/questions/1910794/example.html”>Link</a><b>hello</b>’); $(“#div2″).text(‘<a href=”https://stackoverflow.com/questions/1910794/example.html”>Link</a><b>hello</b>’); }); </script> </head> <body> <div id=”div1″></div> <div id=”div2″></div> </body> </html> A difference that may not be … Read more
My favourite is to extend jQuery with this tiny convenience: $.fn.exists = function () { return this.length !== 0; } Used like: $(“#notAnElement”).exists(); More explicit than using length.
This should work: $(document).ready(function() { $(“someTableSelector”).find(“tr:gt(0)”).remove(); });
Here’s a function for this use case: function getFormData($form){ var unindexed_array = $form.serializeArray(); var indexed_array = {}; $.map(unindexed_array, function(n, i){ indexed_array[n[‘name’]] = n[‘value’]; }); return indexed_array; } Usage: var $form = $(“#form_data”); var data = getFormData($form);
Does it have something to do with preventing other events on the page from firing? Yes. Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called. If you set async: true then that statement will begin it’s execution and the next … Read more
Without using any extra plugins, var myOptions = { val1 : ‘text1’, val2 : ‘text2’ }; var mySelect = $(‘#mySelect’); $.each(myOptions, function(val, text) { mySelect.append( $(‘<option></option>’).val(val).html(text) ); }); If you had lots of options, or this code needed to be run very frequently, then you should look into using a DocumentFragment instead of modifying the … Read more
$(‘#myLink’).click(function(e) { e.preventDefault(); //do other stuff when a click happens }); That will prevent the default behaviour of a hyperlink, which is to visit the specified href. From the jQuery tutorial: For click and most other events, you can prevent the default behaviour – here, following the link to jquery.com – by calling event.preventDefault() in … Read more