Jump to content

[jquery] IMPORTANT: how to don't repeat code


AndyPSV

Recommended Posts

I've attached the code & the question inside: http://jsfiddle.net/AndyPSV/2Pnrh/2/

 

---

 

how to make from those functions (to don't REPEAT THEM in code x10,000 times) -> to put in 1 function

 

--

 

meaning, that you don't repeat: .comment_qC14_ etc, but INSTEAD you've got +ID+ which means ANY VALUE, until its the same

 

or you can launch it from code e.g. using function_name('333',31221); etc.

 

How?

Let's take for a start first two functions.

 

P.S. I think that dynamical attachment of values, e.g.: $('.qC14_,.qC15,.qc16 .... etc').click(function() {

would not make the solution, because I load with AJAX some of those functions & how should I or is it possible to add (or attach) next values to this list?

 

Please help me or teach me by showing/fixing this example, as I learn (in the primary stage) only on examples (& ready code).

 

Thank you.

 

jQuery(function($) {
    $('.qC14_').click(function() {
        $('.qC14').toggle();
    });
    $('.comment_qC14_').click(function() {
        $('.qC14').show();
        $('#comment_qC14').focus();
    });
});

jQuery(function($) {
    $('
a[qrelQ14]').live('
click ', function() {
        var t = $('#comment_qC14 ').get(0);
        if (t.value === "Write a comment...") {
            t.value = "";
        }
        t.value += (t.value ? '' : '') + '@' + $(this).attr('
qrelQ14 ') + ': ';
        t.focus();
        t.selectionStart = t.selectionEnd = t.value.length;
    });
});


jQuery(function($) {
    $('#formQ14 ').submit(function(e) {
        var $f = $(this);

        function comment_saved(serv_response) {
            serv_response = ' < div > ' + serv_response + ' < /div>';
            $f.get(0).reset();
            $f.removeClass('error');
            $(".comment_blockQ14_cnt").text(parseInt($(".comment_blockQ14_cnt").text()) + 1);
            $('#unlock_Q14comment_block').show();
            $('.add_comment_signQ14').hide();
            $(".comment_blockQ14_cntTEMP").text(parseInt($(".comment_blockQ14_cntTEMP").text()) + 1);
            $(serv_response).hide().appendTo($('.comment_blockQ14')).fadeIn(250);
        }
        function err_saving() {
            $f.addClass('error');
        }
        $.ajax({
            url: "http:/ / localhost / ",
            type: "
post ",
            data: $f.serialize()
        }).done(comment_saved).fail(err_saving);
        return false;
    });
});
jQuery(function() {
    $('#view_all_cQ14_').click(function() {
        $('#view_all_cQ14').empty().html('<div class="
comment " style="
padding - left: 10px "><img src="
http: //localhost/tpl/i/i/ico/loader.gif" style="margin-top:6px" alt=""/></div>');
        $('#view_all_cQ14').load('http://localhost//c/q/14');
        $('#view_all_cQ14_Hide').hide();
    });
}); < /script><script type='text/javascript'>function setConfirmUnload(on) {window.onbeforeunload = on ? unloadMessage : null}function unloadMessage() {return '
Are you sure you want to delete comment ? '}$(document).ready(function() {$('.d_comment ').click(function() {setConfirmUnload(true)});$('
form ').submit(function() {setConfirmUnload(false)});});</script>

Could you show me on example, how to re-write below function to use this code?

 

jQuery(function($) {
    $('.qC14_').click(function() {
        $('.qC14').toggle();
    });
    $('.comment_qC14_').click(function() {
        $('.qC14').show();
        $('#comment_qC14').focus();
    });
});

 

Thank you.

Very interesting concept, few minutes ago I was rather thinking about function(ID,something) { }, then manually launch it: function(5,10), function(6,10) [in foreach], but I start to see it

 

I'm however struggling to see, what or rather How it can be user where there are no parents & childrens classes (however I can't find one in the example)?

+ adding to the previous topic, could show show me on example, how to do code:

 

---------- (this part)

jQuery(function($) {
    $('a[arel2]').live('click', function() {
        var t = $('#comment_aC2').get(0);
        if (t.value === "Write a comment...") {
            t.value = "";
        }
        t.value += (t.value ? ' ' : '') + '@' + $(this).attr('arel2') + ': ';
        t.focus();
        t.selectionStart = t.selectionEnd = t.value.length;
    });
});

 

 

here:  http://jsfiddle.net/AndyPSV/QR4GF/9/ (click on "REPLY", bottom or the top one)

 

(it stopped to work, when I've tidied up, the code)

 

 

I would be very thankful.

 

 

 

-------------------------------------------------------------------------------------------------------------

 

I would use, code below (as it allows more flexibility) [only an example]:

 

function zainicjalizujUsuwanie (id) {
    $(".tRemove6_" + id).click(function() {
        $.get("http://localhost//xt/" + id + "/6").done(function() {
            $(".topicRemove6_" + id).remove();
            $("#add_topics_6").text($(".qtopics:visible").length ? "Edit" : "Add");
        }).fail(function() {
            alert("Błąd");
        });
    });
}

jQuery(function ($) {
    zainicjalizujUsuwanie(1);
    zainicjalizujUsuwanie(123);
    zainicjalizujUsuwanie(1024);
   ...
});

 

 

THANK YOU for help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.