L
L
lavezzi12015-10-05 07:41:23
css
lavezzi1, 2015-10-05 07:41:23

How to rewrite this code and organize the function?

Hello colleagues. Recently started working with jQuery and JS in general. I wrote this code:

The bottom line is that when you click on the input, a button pops up. I wonder how you can shorten / rewrite the code? Considering that there will be 20 such inputs.

And why doesn't the on('change') function work? If I want the button to appear only when the input is written.

I will be very grateful for the answers and recommendations!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kirill Turovnikov, 2015-10-05
@turovnikoff

For example like this codepen.io/anon/pen/MapRxw

V
Vitaly Inchin ☢, 2015-10-05
@In4in

Hang a common handler on the form and combine all buttons with 1 class ( .send_button ). Buttons, by the way, should be placed in .form_group , it will be more logical. And then:

$("form").on("focus", ".form_group > input", function(){
   $(this).next(".send_button").fadeIn();
}).on("blur", ".form_group > input", function(){
   $(this).next(".send_button").fadeOut();
});

“When they wrote in input” is oninput , and onchange is change + loss of focus.

A
Alexey Tsarapkin, 2015-10-05
@Dreamka

codepen.io/anon/pen/zvZXbo
Maybe keydown is better?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question