How to get the value of the current element(this)?

There is a button which creates a div containing table, input field and button. By pressing Inputa added rows to the table with its contents. Tell me how to get the value of inputa to the block in which it is located ? and it turns out that if more of the 2nd block, it is added only to the first all. Using id all turns out but as far as I know, it must be unique. Tell me how to resolve this issue. Tried this way, var valueOfTask = $(this).closest("input") - not published
August 19th 19 at 23:09
1 answer
August 19th 19 at 23:11
Solution
Do not fully understand where you want to.
By clicking or the like.
As I understand it, you need to know the value of the input, regardless of the number of the same div.
If the event is something like this:
Let everyone ADD there is a class buttonAdd.
Show an example with a handler on body and delegation.
var body=$("body");

body.on("click", function(e){
 var bittonAdd=$(e.target).closest(".buttonAdd");
 if(!bittonAdd.length) return false;
/*
length check is needed, because hasClass checks for class, and the closest, if you do not find a suitable parent will return an empty object (which when converted to Boolean is true).
*/
 var div = bittonAdd.closest("div");//If div has class, bittonAdd.closest(".className");
 var input = div.find("input");
 var value = input.val();//This is the value.
//Add to the table:
 div.find("table").append(""+value+"here are your buttons");
/*
Not children, if input is a direct descendant, you can children, but if not - then he will not find it
*/
});


Detail painted to make it clear.

To speed, can make the template row for a table:
body.on("click", function(e){
 var bittonAdd=$(e.target).closest(".buttonAdd");
 if(!bittonAdd.length) return false;
 var div = bittonAdd.closest("div");
 var value = div.find("input").val();//This is the value.
 var tr = hideLine.clone(true);
tr.find("td").eq(0).text(value);
div.find("table").append(tr);
});

//the same template:

var hideLine = $("your button here");
Thank you very much! All is lucidly clear, understood, works. - Joshuah_Ort commented on August 19th 19 at 23:14

Find more questions by tags CSSHTMLJavaScriptjQuery