Jump to content
elcaiaimar

call javascript html class from another javascript file

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!

 

Share this post


Link to post
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/

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...