0

in my web i have many button that javascript for managing click function. Every button click has class for initial each of them. here the code

<script type="text/javascript">
$(document).ready(function(){
    $(".data1").click(function(){
        $("#line").val("1");
        $("#pkategori").val("-Kategori Produk-");
        $("#pname").val("-Nama Produk-");
        $("#pnumber").val("");
        $("#seri").val("");
        $("#quantity").val("");
        $("#ok").hide();
        $("#cancel").hide();
        $("#tnposisi").hide();
        $("#nposisi").hide();
        $("#tketp").hide();
        $("#ketp").hide();
        $("#tpjumlah").hide();
        $("#pjumlah").hide();
        $("#editsave").hide();
        $("#editcancel").hide();
        $("#input").hide();
        $("#loading").hide();
        $("#status").html("");
    });
});

for my current web, i make this one by one until $(".data160").click(function(). it will be long code and not effective. My problem, How tomake this a simple code?

2 Answers 2

1

Give all the buttons the same class, and use data attributes for the different features.

<button class="data" data-line="1" data-kategory="-Kategori Produk-" data-name="-Nama Produk-">

Then write:

$(".data").click(function(){
    $("#line").val($(this).data("line"));
    $("#pkategori").val($(this).data("kategori"));
    $("#pname").val($(this).data("name"));
    $("#pnumber,#seri,#quantity").val("");
    $("#ok,#cancel,#tnposisi,#nposisi,#tketp,#ketp,#tpjumlah,#pjumlah,#editsave,#editcancel,#input,#loading").hide();
    $("#status").html("");
});

To handle them all at once.

Sign up to request clarification or add additional context in comments.

Comments

1

separate it with several functions:

1、val and html together

2、hide together, you can create a function like this

    function hideEl(el){
      $('#'+el).hide();

    }
   var arrEls = ['pname','apple','msn'...];
   $(arrEls).each(function(key,value){
      hideEl(value);
   });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.