0

I have a page with some inline javascript in it

<script type="text/javascript">
            $('#clipboardContainer').hide();
            $( '#clipboard' ).click(function() {
                $("#clipboardContainer").toggle( "slide", {direction: "up", mode: "hide"}, "slow" );
                $("#reportsContainer").toggle();

                if ($("#clipboard").html() == 'Open Clipboard')
                    $("#clipboard").html('Close Clipboard');
                else
                    $("#clipboard").html("Open Clipboard");
            });
            $('#prev').click(function() {
                $.ajax({
                    type: "GET",
                    url: "/teams/change_date",
                    data: {
                        'id': '<%= @team.id %>',
                        'date': '<%= @date - 1.day %>'
                    }
                });
            });
            $('#next').click(function() {
                $.ajax({
                    type: "GET",
                    url: "/teams/change_date",
                    data: {
                        'id': '<%= @team.id %>',
                        'date': '<%= @date + 1.day %>'
                    }
                });
            });
        </script>

After an ajax call, I want to modify the 'date' attributes using Jquery. How do I reference the javascript functions using the DOM?

3
  • 2
    There is no DOM tree for <script> contents, it's just character data. Even if there was, it wouldn't affect the run-time in-memory objects. Commented Apr 14, 2013 at 0:47
  • Is date an unix timestamp or some form of representation? Commented Apr 14, 2013 at 0:53
  • yes, date is a string representation of a Rails Date object Commented Apr 14, 2013 at 15:22

1 Answer 1

2

As stated by Esailija there is no way to use DOM functions to manipulate javascript code, what you are needing is to implement variables that you modify after your ajax has successfully be called:

First off pull your ajax data objects out into variables:

/// pull your ajax data objects out into vars, best not to
/// pollute the global namespace though, as is done here,
/// if you can help it. I've kept things like this for simplicity.
var nextdata = {
  type: "GET",
  url: "/teams/change_date",
  dataType: 'json',
  data: {
    'id': '<%= @team.id %>',
    'date': '<%= @date - 1.day %>'
  }
};

var prevdata = {
  type: "GET",
  url: "/teams/change_date",
  dataType: 'json',
  data: {
    'id': '<%= @team.id %>',
    'date': '<%= @date + 1.day %>'
  }
};

Then create two new functions which will get called after your ajax calls have returned successfully.

/// these two functions are just examples to illustrate
/// they will alter the ajax data objects when required
/// that is as long as your change_date script returns json
/// containing a 'newdate' property in your expected format.
var nextcallback = function( resp ){
  nextdata.date = resp.newdate;
};

var prevcallback = function( resp ){
  prevdata.date = resp.newdate;
};

Then modify your original code as such:

$('#clipboardContainer').hide();
$( '#clipboard' ).click(function() {
  $("#clipboardContainer").toggle( "slide", 
    {direction: "up", mode: "hide"}, "slow" );
  $("#reportsContainer").toggle();
  if ($("#clipboard").html() == 'Open Clipboard') {
    $("#clipboard").html('Close Clipboard');
  }
  else {
    $("#clipboard").html("Open Clipboard");
  }
});
$('#prev').click(function() {
  $.ajax(prevdata).done(prevcallback);
});
$('#next').click(function() {
  $.ajax(nextdata).done(nextcallback);
});

global namespace

With regards to my comments about avoiding polluting global namespace, you should be wrapping your code, first in a "no conflict" anonymous wrapper, and second in a dom ready wrapper — like so:

;(function($){ /// <-- no conflict wrapper

  /// place your methods here

  $(function(){ /// <-- dom ready wrapper

    /// place your jQuery DOM manipulation here

  });

})(jQuery);

That way you keep your code segregated from interfering with anything else, you do need to be aware that your functions wont be accessible outside of these wrapper functions though — unless you've attached them as event listeners or exposed them in some other way globally.

Or as Ohgodwhy has accurately pointed out you could save yourself a function wrapper with:

jQuery(function($){ /// <-- dom ready + no conflict wrapper
  /// place your functions + jQuery here
});
Sign up to request clarification or add additional context in comments.

2 Comments

Lots of good work here, one thing to note, jQuery(function($){ is a no-conflict ready function, you can avoid the double redundancy here.
@Ohgodwhy Nice point, I had not realised, thanks for pointing it out :)

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.