I wrote this code here:

$(document).ready(function(){ $(".button1").on(function() { var e1 = $(this).data("target1"); $(e1)toggleClass("rectangle1--open"); }) }); 

- That's right connected, but why it does not work ?? Tell me please!!!

Closed due to the fact that off-topic participants LEQADA , Saidolim , Kromster , Viktor Tomilov , 0xdb 6 Feb '18 at 16:53 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - LEQADA, Saidolim, Kromster, Viktor Tomilov, 0xdb
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    $(".button1").on(function() what's on ? There should be an event - Rostyslav Kuzmovych

1 answer 1

if you need to click on an element then $(".button1").click(...) . Inside the function there are also typos.

 $(function(){ $(".button1").click(function() { var e1 = $(this).data("target1"); $(e1).toggleClass("rectangle1--open"); }) }); 
 #t { width: 200px; height: 50px; border: 2px solid grey; text-align: center; line-height: 50px; } #t.rectangle1--open { background-color: orange; color: white; font-weight: bold; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" class="button1" data-target1="#t">тест</a> <div id="t">toggle</div>