views:

47

answers:

1

I have a function for the class .myclassA, inside this function I capture the id of the particular element chose and I put it inside a variable inputid. This function also brings another function for another class(.myclassB), which is inside the first function. Do you guys have any idea how I can pass the variable inputid from the first function to the function inside it? Thanks for all your help

$('.myclassA').click(function(){
  var inputid = $(this).attr('id');
  $('.myclassB').click(function(inputid){
      var thisid = $(this).attr('id');
      $(inputid).val(thisid);
  });
  //$('seqa').click();
});
//$('#empcriddi').focus();
A: 

You don't need to "pass" it, just don't name the local variable with the same name, like this:

$('.myclassA').click(function(){
  var inputid = $(this).attr('id');
  $('.myclassB').click(function(){ 
      var thisid = $(this).attr('id');
      $(inputid).val(thisid);
  });
});

Though this won't quite work either, and there's no reason to go a lookup of an element you already have so just maintain a reference, for example:

$('.myclassA').click(function(){
  var input = $(this);
  $('.myclassB').click(function(){ //you may want to also .unbind('click') here
    input.val(this.id);
  });
});
Nick Craver
This works great!
El Fuser