AndyPSV Posted December 8, 2011 Share Posted December 8, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/252770-jquery-important-how-to-dont-repeat-code/ Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 I think you want something like this: http://jsfiddle.net/kgeHe/ Quote Link to comment https://forums.phpfreaks.com/topic/252770-jquery-important-how-to-dont-repeat-code/#findComment-1295866 Share on other sites More sharing options...
AndyPSV Posted December 8, 2011 Author Share Posted December 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252770-jquery-important-how-to-dont-repeat-code/#findComment-1295870 Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 I could help more if you showed me the markup in which you are using this, but maybe this will get you started: http://jsfiddle.net/kgeHe/1/ Quote Link to comment https://forums.phpfreaks.com/topic/252770-jquery-important-how-to-dont-repeat-code/#findComment-1295884 Share on other sites More sharing options...
AndyPSV Posted December 8, 2011 Author Share Posted December 8, 2011 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)? Quote Link to comment https://forums.phpfreaks.com/topic/252770-jquery-important-how-to-dont-repeat-code/#findComment-1295894 Share on other sites More sharing options...
AndyPSV Posted December 8, 2011 Author Share Posted December 8, 2011 + 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. Quote Link to comment https://forums.phpfreaks.com/topic/252770-jquery-important-how-to-dont-repeat-code/#findComment-1295901 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.