I'm modifying the content of the modal window that pops up when using Linkit module in a WYSIWYG.
This modal uses AJAX so I have to use Drupal.behaviors for the first time. My code is structured basically like this:
(function ($) {
Drupal.behaviors.mymoduleLinkit = {
attach: function (context, settings) {
$('#linkit-dashbaord-ajax-wrapper').once('linkitSelects', function() {
//do cool stuff
});
}
}
})(jQuery)
Now, I need to get the existing value of an input box here, so I simply do this:
var $existingData = $('.form-item-linkit-rel input').val();
Then try to return that value, but it's always undefined. I think it's because this page seems to do a few AJAX calls in a row and I am trying to get the value before it is actually loaded into that field and available.
To ensure that waiting until AJAX is loaded is actually the problem I wrapped my code in a timeout() in order to wait for the loading to finish. This isn't a good solution, but it confirms everything is working once things are loaded. It looked like this:
//wait so data is loaded via ajax
setTimeout(function() {
//set our select list to any pre-existing values
var existingData = $('.form-item-linkit-rel input').val();
if(existingData != ""){
var $parts = existingData.split('_');
$('.form-item-linkit-rel .size-select').val($parts[0]);
$('.form-item-linkit-rel .type-select').val($parts[1]);
};
}, 500);
So the question is: Instead of using a timeout, how can I fire this code only after the initial ajax calls are done?
You can view the full code here including the timeout stuff but please don't get distracted by all the other stuff going on (or my poor coding)...Everything else in here works and I'm really just concerned with getting the data in the question above.
var $existingData
- shouldn't it bevar existingData
? Why$
? Looks like you are asking for conflict with jQuery.$
. I always do it, makes it easier to code when you're doing drupal/php :)