1
I am developing an application in which I have some "views" that are loaded in my <section id="view">
through ajax.
The view loads normally, but functions that have been programmed with jQuery for all inputs, for example, do not work. As far as my logic has allowed me to observe, this happens by elements added by ajax being added to the DOM later to the main script - which takes care of the inputs - being loaded, causing it not to notice the new elements.
I am currently copying and pasting the tag script
in all views, which is being a bag for maintenance and will be unviable when launching the app, since I will need to minify everything in one app.min.js
.
What would be the solution to my case?
Remembering that in case I change the attr('src')
from the script, Chrome warns me via console that it’s an obsolete way to do this.
Edit:
Assuming this is mine main.js
//Selector personalizado
//Nenhum select option vai funcionar sem isso!
$('.selector-input').click(function(){
var selector = $(this).children('.selector');
var pickerIcon = $(this).find('.input-text .picker .picker-icon .material-icons');
if(selector.css('display') == 'none'){
selector.fadeIn(100);
pickerIcon.addClass('rotated');
}
});
$('.selector .options .option').click(function(){
var content = $(this).text();
var optId = $(this).attr('data-opt');
var inputId = $(this).parents('.selector').attr('data-picker-id');
$('#' + inputId).text(content);
$('#' + inputId).attr('data-selected', optId);
$(this).parents('.selector').fadeOut(100);
$('.material-icons').removeClass('rotated');
});
$(function(){
//Caso seja empregador
if($('#view2')){
view = getUrlParameter('view') ? getUrlParameter('view') : window.location.href+="?view=newusers"
$.ajax({
method: "post",
url: "views/empregador/" + view + ".php",
data: {
auth: 'loadedbyajax'
}
}).done(function(data) {
$('#view2').html(data);
});
$('.user-profile-picture').each(function(){
$(this).css('background-image', "url('" + $(this).attr('data-bg') + "')");
});
}
});
And this is the server response (which will be added to my <section id="view2">
:
<div class="selector-input">
<div class="input-text">
<label>Estado</label>
<span class="picker">
<span id="estadoSelect" class="picked" data-selected="ES">
Espírito Santo</span>
<span class="picker-icon"><i class="material-icons">keyboard_arrow_down</i></span>
</span>
</div>
<div class="selector" data-picker-id="estadoSelect">
<div class="options">
<div class="option" data-opt="DF">Distrito Federal </div><div class="option" data-opt="ES">Espírito Santo</div><div class="option" data-opt="MG">Minas Gerais</div><div class="option" data-opt="RJ">Rio de Janeiro</div><div class="option" data-opt="SP">São Paulo</div> </div>
</div>
</div>
@dvd It was an example. The main focus is: how to force the Reload of a script in the right way.
– Daniel Bonifácio
@dvd added the way I am making the requests and loading my view in the question.
– Daniel Bonifácio
O . click() of
$('selector-input')
- loaded by ajax. The ones that already exist in the page work perfectly.– Daniel Bonifácio