I have these functions:    $('.list-group .list-group-item.list-group-item-action').click(function(e) {     $('.list-group .list-group-item.list-group-item-action.active').removeClass('active');     var $this = $(this);     if (!$this.hasClass('active')) {         $this.addClass('active');     }     e.preventDefault();   });    document.querySelectorAll('.list-group-item').forEach(function(channel){     channel.onclick = () =&gt;{       load_channel(channel);       return false;     }   });     socket.on('create channel', data =&gt;{     const button = document.createElement('button');     button.className = "list-group-item list-group-item-action";     button.innerHTML = `${data.channel}`;     document.querySelector('.list-group').append(button);    });   The last function dynamically creates a button in this list (ignore the jinja):    &lt;div class="list-group"&gt;          &lt;button class="list-group-item list-group-item-action active"&gt;General&lt;/button&gt;         {% for channel in channels %}         &lt;button class="list-group-item list-group-item-action"&gt;{{ channel }}&lt;/button&gt;         {% endfor %}       &lt;/div&gt;   But when it's created, the first two functions do not work.  What is the solution?
112
I have a bit of code where I am looping through all the select boxes on a page and binding a .hover event to them to do a bit of twiddling with their width on mouse on/off.  This happens on page ready and works just fine.  The problem I have is that any select boxes I add via Ajax or DOM after the initial loop won't have the event bound.  I have found this plugin (), but before I add another 5k to my pages with a plugin, I want to see if anyone knows a way to do this, either with jQuery directly or by another option.
56223
Any idea why the piece of code below does not add the script element to the DOM?  var code = "&lt;script&gt;&lt;/script&gt;"; $("#someElement").append(code);
7091
I know that jQuery is loaded, because I can switch out the $ for 'jQuery' and everything behaves as expected, but this will be a messy script if I can't fix this  This script:  jQuery(document).ready(function(){     $("ul.vimeo_desc_feed li a").click(function(){         alert($(this).attr('href'));         return false;     }) });   Produces the error $ is not a function  This script:  jQuery(document).ready(function(){     jQuery("ul.vimeo_desc_feed li a").click(function(){         alert(jQuery(this).attr('href'));         return false;     }) });   works fine.
83258
Does anybody know of a method to trigger an event in Prototype, as you can with jQuery's trigger function?  I have bound an event listener using the observe method, but I would also like to be able to fire the event programatically.  Thanks in advance
104708
I'm working on a web page where I'm making an AJAX call that returns a chunk of HTML like:   &lt;div&gt;   &lt;!-- some html --&gt;   &lt;script type="text/javascript"&gt;     /** some javascript */   &lt;/script&gt; &lt;/div&gt;   I'm inserting the whole thing into the DOM, but the JavaScript isn't being run.  Is there a way to run it?    Some details: I can't control what's in the script block (so I can't change it to a function that could be called), I just need the whole block to be executed.  I can't call eval on the response because the JavaScript is within a larger block of HTML.  I could do some kind of regex to separate out the JavaScript and then call eval on it, but that's pretty yucky.  Anyone know a better way?
92658
I'm using Jquery's toggle event to do some stuff when a user clicks a checkbox, like this:  $('input#myId').toggle( function(){ //do stuff   }, function(){ //do other stuff     } );   The problem is that the checkbox isn't being ticked when I click on the checkbox. (All the stuff I've put into the toggle event is working properly.)  I've tried the following:  $('input#myId').attr('checked', 'checked');   and  $(this).attr('checked', 'checked');   and even simply  return true;   But nothing is working. Can anyone tell me where I'm going wrong?  Edit - thanks to all who replied. Dreas' answer very nearly worked for me, except for the part that checked the attribute. This works perfectly (although it's a bit hacky)  $('input#myInput').change(function () {     if(!$(this).hasClass("checked"))     {         //do stuff if the checkbox isn't checked         $(this).addClass("checked");         return;     }      //do stuff if the checkbox isn't checked     $(this).removeClass('checked'); });   Thanks again to all who replied.
121420
I have put together the following mootools script   window.addEvent('domready', function() {     var shouts = "timed.php";     var log = $('log_res');     function updateData (url,target)     {         new Ajax(url,{         method: 'get',          update: $(target),          onComplete: function() {         log.removeClass('ajax-loading');}  }).request();         log.empty().addClass('ajax-loading');     }      var update = function(){ updateData ( shouts, 'log_res' ); };     update();           // call it immediately     update.periodical(10000);   // and then periodically   });   heres the html  &lt;div id="AJAX"&gt;     &lt;h3&gt;Ajax Response&lt;/h3&gt;     &lt;div id="log_res"&gt;exercise&lt;/div&gt; &lt;/div&gt;   its using moo 1.1.  The above works fine, the page loads then the ajax request kicks in div id log_res has a class of ajax-loading whilst its updating, and when its finished the text exercise in the div is replaced with whatever the ajax has returned (yippee). But I want to put some custom HTML into the div WHILST the page is loading, because the ajax-loading class is not enough to contain the information, i also want to put a spinny flasher into the div whilst the ajax request is retrieving the information. Hope that makes sense!
123109
Say I am extending The blockBase class to create a custom block, and I am implementing the blockBuild method to create some markup for my block. Something like this:   class YourModuleBlock extends BlockBase {     /**      * Implements \Drupal\block\BlockBase::blockBuild().      */      public function build() {       return array(         '#markup' =&gt; 'This is a block!',       );      }    }   If I wanted to provide a link in this markup, in D7 I would have used the l function, but this is no longer available (). So, I need to make available the new D8 method for generating links. I could just use an &lt;a&gt; tag, but this was never best practice in D7.  So, what is the right method to use for creating links and how do I make it available to my function?
11598
Can anyone help me? Trying to do very simple function to navigate from component to another. I copied the code from Developers guide, but for some reason it doesn't work for me.  My function to navigate to another component:  nav : function(component, event, helper) {             var evt = $A.get("e.force:navigateToComponent");             evt.setParams({                 componentDef : "c:navToComponentTest2",                 componentAttributes: {                     continent : component.get("v.continent")                 },                 isredirect : false             });             evt.fire();     }   And here's the error I get in my Lightning App   EDIT: Got it working in Lightning tab. Would it be somehow possible to get it working in communities as well? Or is there anyway to make same functionality?
72086
I'm refactoring some old JavaScript code and there's a lot of DOM manipulation going on.  var d = document; var odv = d.createElement("div"); odv.style.display = "none"; this.OuterDiv = odv;  var t = d.createElement("table"); t.cellSpacing = 0; t.className = "text"; odv.appendChild(t);   I would like to know if there is a better way to do this using jQuery. I've been experimenting with:  var odv = $.create("div"); $.append(odv); // And many more   But I'm not sure if this is any better.
9826
I have a javascript button on my product layout named "ListToEbay" and I have written a controller to invoke the EndItem()  service method of ebay api.  Below is the method:--  public void  removeItemFromEbayList() {   HttpRequest req = new HttpRequest();   Http http = new Http();   sEnvelopHandlerForEndingList='Request XML';   req.setBody(sEnvelopHandlerForEndingList); }   I want to execute this method directly on clicking that button, without going or opening a VisualForce Page. How can I accomplish that?
94608
I have a list . I want select items of list with a special quary.(get items that created with current user). Then get count of items.  I use this code for get current user :  function Initialize() {     context = new SP.ClientContext.get_current();     web = context.get_web();    var currentUser = web.get_currentUser();    alert(currentUser);     currentUser.retrieve();     context.load(web);     context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),           Function.createDelegate(this, this.onFailureMethod)); }  function onSuccessMethod(sender, args) {     var userObject = web.get_currentUser();     alert('User name:' + userObject.get_title() + '\n Login Name:' +               userObject.get_loginName()); } function onQueryFailed(sender, args) {     alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); }   But don't get current user.  and use this code for get items of list.   function Initialize() {     clientContext = new SP.ClientContext.get_current();     web = clientContext.get_web();      var list = web.get_lists().getByTitle("Sends");     var camlQuery = new SP.CamlQuery();     var q = "&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='CreatedUser' /&gt;&lt;Value Type='User'&gt;CurrentUser&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;";     camlQuery.set_viewXml(q);     this.listItems = list.getItems(camlQuery);      clientContext.load(listItems, 'Include(ID)');      clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),      Function.createDelegate(this, this.onQueryFailed)); } function onListItemsLoadSuccess(sender, args) {     var count = 0;       count = this.listItems.get_count();             }  function onQueryFailed(sender, args) {     alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); }
59846
In the following example code, I attach an onclick event handler to the span containing the text "foo". The handler is an anonymous function that pops up an alert().  However, if I assign to the parent node's innerHTML, this onclick event handler gets destroyed - clicking "foo" fails to pop up the alert box.  Is this fixable?  &lt;html&gt;  &lt;head&gt;  &lt;script type="text/javascript"&gt;    function start () {     myspan = document.getElementById("myspan");     myspan.onclick = function() { alert ("hi"); };      mydiv = document.getElementById("mydiv");     mydiv.innerHTML += "bar";   }   &lt;/script&gt;  &lt;/head&gt;   &lt;body onload="start()"&gt;    &lt;div id="mydiv" style="border: solid red 2px"&gt;      &lt;span id="myspan"&gt;foo&lt;/span&gt;    &lt;/div&gt;  &lt;/body&gt;  &lt;/html&gt;
25453
I have created a 'control' using jQuery and used jQuery.extend to assist in making it as OO as possible.  During the initialisation of my control I wire up various click events like so  jQuery('#available input',              this.controlDiv).bind('click', this, this.availableCategoryClick);   Notice that I am pasing 'this' as the data argument in the bind method. I do this so that I can get at data attached to the control instance rather from the element that fires the click event.   This works perfectly, however i suspect there is a better way  Having used Prototype in the past, I remember a bind syntax that allowed you to control what the value of 'this' was in the event.  What is the jQuery way?
46669
I have two simple functions that load stuff using  and , something like these:  function admin_custom_css() { wp_enqueue_style( 'stylesheet_name', 'stylesheet.css') };   function admin_custom_js  { wp_enqueue_script( 'javascript_file', 'script.js') };   ... and a few admin pages, created with  and   function my_menu() {    add_menu_page('Page 1', 'bar', 'something', 'else', 'foo');    add_submenu_page( 'theme_menu', 'Subpage 1', 'Subpage', 'something', 'else', 'foo');  } add_action('admin_menu', 'my_menu');    How do I load my two functions only on these pages?  Right now I'm using:  add_action('admin_init', 'admin_custom_css' );  add_action('admin_init', 'admin_custom_js' );     But it loads my files on every admin page, which is not nice at all.  Can I do this via one simple line in functions.php or have to enqueue them within my pages separately (I prefer the first option strongly, since I'd have to edit a lot of admin-page-creating-functions).  Thanks!
38242
I have a visualforce page which on load should call 4 methods one after the other. For that purpose, I tried using apex:actionFunction  &lt;apex:page sidebar="false" showHeader="false" controller="PullFromTaleo"&gt;   &lt;apex:form &gt;     &lt;apex:actionFunction name="Method1" action="{!Method1}" immediate="false"/&gt;     &lt;apex:actionFunction name="Method2" action="{!Method2}" immediate="false" /&gt;     &lt;apex:actionFunction name="Method3" action="{!Method3}" immediate="false"/&gt;     &lt;apex:actionFunction name="Method2.1" action="{!Mehtod2}" immediate="true"/&gt;     &lt;apex:actionFunction name="Method4" action="{!Mehtod4}" immediate="true"/&gt;  &lt;/apex:form&gt; &lt;/apex:page&gt;   However none of the above functions are executed. Not sure how to call multiple functions in sequence from a visualforce page.
71754
$(function(){   $('.slider').mobilyslider({     content: '.sliderContent',     children: 'div',     transition: 'horizontal',     animationSpeed: 500,     autoplay: true,     autoplaySpeed: 9000,     pauseOnHover: true,     bullets: true,     arrows: true,     arrowsHide: true,     prev: 'prev',     next: 'next',     animationStart: function(){},     animationComplete: function(){}   });    });   I am integrating the above JavaScript file in a Drupal theme, but it is giving me the "$ is not function" error. I googled it, and changed the code bro the below, but it doesn't work either.  (function($) {   $('.slider').mobilyslider({     content: '.sliderContent',     children: 'div',     transition: 'horizontal',     animationSpeed: 500,     autoplay: true,     autoplaySpeed: 9000,     pauseOnHover: true,     bullets: true,     arrows: true,     arrowsHide: true,     prev: 'prev',     next: 'next',     animationStart: function(){},     animationComplete: function(){}   });    })(jQuery);   The above code gives me following error:     $('.slider').mobilyslider is not a function