SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    call a function in a if statement (banging my head)

    I've been googling and googling and googling…*it all makes sense but none of it works. All I need to do is to be able to call a function from within an if statement that is in a click function.

    Here's the setup
    Code JavaScript:
    	$('#new_task_button').click(function() {
    		if ($('#project_start_date').val() === "" && $('#project_due_date').val() === "") {
    			$('body').append('<div id="notice"><a href="#" class="close_notice recessed_button"><img src="/images/trans_x.png" /></a><h2>Form Error</h2><div class="content"><p>Please provide a project start and end date first.</p></div></div>');
    		} else {
    			//this is where i would want the duplicateField() to fire for the #new_task_button
    		}
    		return false;
    	});

    This was/is working just fine before like so

    Code JavaScript:
    	$('#new_task_button').click(duplicateField);

    I feel like I should be able to do something close to "$(this).duplicateField();" but my function isn't setup to extend jquery so I know that's not the answer.

    Any help is appreciated. Thanks.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,900
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Could you post the code for the duplicateField function?
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    $('#new_task_button').click(function() {
            if ($('#project_start_date').val() === "" && $('#project_due_date').val() === "") {
                $('body').append('<div id="notice"><a href="#" class="close_notice recessed_button"><img src="/images/trans_x.png" /></a><h2>Form Error</h2><div class="content"><p>Please provide a project start and end date first.</p></div></div>');
            } else {
                duplicateField.call(this); // alternative 1
                duplicateField(this); // alternative 2
            }
            return false;
        });

    By using call, you make sure this is preserved. Now you can do this:

    Code javascript:
    function duplicateField(field) {
      var new_task_button = $(this); // alternative 1
      var new_task_button = $(field); // alternative 2
    }

  4. #4
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    O…M…G… Thanks SO much, Raffles.

    apparently .call() is what I was searching for (but didn't know it). And oddly enough it was on the '5 ways to call a function' page I was reading. Apparently I didn't know what I was looking at.

    Since Scallio asked and in case anyone else searching has the same issue, here's my duplicate function
    Code JavaScript:
    	function duplicateField() {
    		var assoc   = $(this).attr('data-association');
        	var content = $('#' + assoc + '_fields_template').html();
        	var regexp  = new RegExp('new_' + assoc, 'g');
    		var new_id = $(this).closest('div.listing_box').find('div.form_row').length
     
        	$(this).closest('div.listing_box').find('fieldset').append(content.replace(regexp, new_id));
        	return false;
    	}

    Once again…*THANK YOU for the help!
    your brain reacts in the same way whether you are
    looking at something or thinking about it...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •