Jump to content

call javascript html class from another javascript file


elcaiaimar

Recommended Posts

Hi, I having problems when I execute (from a JS file) an onclick function by the class that is into an append of another JS file.

 

My code is this:

 

JS File1:

 

var user_tmpl = $('<div />')
      .addClass('user')
      .append('<strong/>').find('strong').addClass('titulo').html(item.titulo)
      .append('<a href="#buscamedioModal" role="button" class="btn btn-danger deletebuscamedio" data-toggle="modal" id=' + item.gid + ' data-name=' + item.titulo + '>Eliminar</a>')
      .parent();

 

 

JS File2 (call):

 

// Configuraciones Generales
var boton_eliminar_buscamedio = ".deletebuscamedio"; // Clase

// Fin de configuraciones
$(document).on('ready',function(){
  $(boton_eliminar_buscamedio).on('click',function(e){
    e.preventDefault();
    var Pid_medio = $(this).attr('id');
    alert(Pid_medio);
    var name_buscamedio = $(this).data('name');

But it seems that it can't run the function because doesn't find the class "deletebuscamedio".

Does anyone how to do this?

Thank you in advance!

 

Link to comment
Share on other sites

The elements don't exist when you're trying to add a click handler to them. Instead of trying to add the click handler to those elements you should use delegation. Attach the click event to the body element, for example, and delegate it to elements with that class. So when you click it will run the click handler on the body and search for elements with that class to see if one of them was clicked.

 

http://api.jquery.com/on/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...