tshoemake tshoemake - 1 year ago 87
HTML Question

Getting the selector of $this

I basically am trying to copy my selector and create a new one.. this is what im trying to achieve...

I have many inputs in divone and dont want to list out each one in my jquery, so i would like to dynamically find the object and link to the corresponding divtwo object

<div id="divOne">
<input type="textbox" id="tb1" value="TEXTHERE">
<input type="textbox" id="tb2" value="TEXTHERE">
<div id="divTwo">
<input type="textbox" id="tb3" value="TEXTHERE">
<input type="textbox" id="tb4" value="TEXTHERE">

$("#divOne:input").bind("keyup change", function(e) {
var selector = ($(this).selector()); //hoping this would return input object from DivOne
var value = ($(this).val());
var newselector = //modify divOne object to have divTwo selector
$(newselector).val() = value;


Then I'd like to to save divOne text into the divTwo textbox.

I want it to look like this, only using this. So lets say i change textbox value of tb1 to "textthere":

$("#divOne:input").bind("keyup change", function(e) {
var selector = $(#divOne.attr("id", tb1")); //this should be covered dynamically using $this or another jquery method
var newselector = $(#divTwo.attr("id", tb3)); //this however should be generated dynamically by selector var
$(newselector).val() = selector.val();



Answer Source

If you want to match corresponding elements you can use index() and eq()

var $d1Inputs = $("#divOne input").on("keyup",function(e) {
     var index = $d1Inputs.index(this);
     $('#divTwo input').eq(index).val($(this).val()); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download