-2

I made this example code: https://jsfiddle.net/gwpcfp89/

The question is about this click event:

var wrapper = $(".select-editable"); // Fields wrapper

$(wrapper).on('keypress', '#input1', function () {
  var cs = $(this).val().length+1;
  console.log( "total caracters:" + cs);
  $('#mytext1').html('<option value="dummy01">dummy01</option><option value="dummy02">dummy02</option><option value="dummy03">dummy03</option>')
});

I use #input1, #mytext1, and I want the click to be dynamic. For example: if I create four selects in the table, the fourth select should have the click event like the first select (#input4 and #mytext4).

Is it possible?

Badacadabra
  • 5,385
  • 7
  • 23
  • 42
occiso
  • 7
  • 1

3 Answers3

-1
I have modified you JSFiddle code - https://jsfiddle.net/pjf17exa/
As per your code, i have fixed some error and modified you code to make it dynamic input action.

// your code
var wrapper = $(".select-editable"); //Fields wrapper

$(wrapper).on('keypress', '#input1', function () {
var cs = $(this).val().length+1;
  console.log( "total caracters:" + cs);
  $('#mytext1').html('<option value="dummy01">dummy01</option><option 
  value="dummy02">dummy02</option><option 
  value="dummy03">dummy03</option>')
});

-- which assigns event for the current row, not for the dynamic elements. So instead take parent element and assign event for the input. Where you can access current element using $(this) instance inside the function. 

// do like this
var wrapper = $("#table-editable");
wrapper.on('keypress', 'input', function () {
  var cs = $(this).val().length+1;
  console.log( "total caracters:" + cs, $(this));
   $(this).prev().html('<option value="dummy01">dummy01</option><option value="dummy02">dummy02</option><option value="dummy03">dummy03</option>')
});
-1

You can do a trick here.

when you create dynamic select tags, put one attribute to them like : data-bind="<values>"

so here should be your dynamic numbers which you are appending to id to make input4

and then write a jquery function like give here:

$('select').click(function(){
    var clicked_select = $(this).attr('data-bind');
    // clicked_select will be the number which dynamic element you have clicked. You can use that number with id to access that particular element as a new selector.
});

This should be your solution.

Himanshu Upadhyay
  • 6,220
  • 1
  • 13
  • 32
-1

There is a few issues with your code:

  1. The string you were assigning to data+= had some of the HTML attributes using double quotes rather than single quotes, so it was causing the code to break.

  2. You were dynamically creating input fields with the same ID. An ID attribute should always be unique on a page. To fix this I have used the i+1variable to append a unique number to the input ID. Now that the ID is unique I have to changed your $("#students").on('keypress', '#input1', function () { to $("#students").on('keypress', 'input', function () { so it selects the element by it's tag name.

  3. This $(wrapper).on('keypress', 'input', function () { was using .select-editable as the event handler, but the .select-editable was being dynamically generated. The event handler must be an element that wraps around all the elements that are going to be dynamically generated, and not part of a dynamically generated element. So I changed that part of the code to use #students instead.

I hope this helps explain what the issues were.

https://jsfiddle.net/gwpcfp89/6/

WizardCoder
  • 2,876
  • 7
  • 19