0

I'm migrating files from prototype to jQuery.

prototype:

function hideEditableMarkers() {
   $$('.edit_marker').each(function(el) {
  el.hide();
});
   $$('.show_marker').each(function(el) {
    el.show();
    });
}

Event.observe(window, 'load', hideEditableMarkers);

jQuery:

function hideEditableMarkers() {
  jQuery('.edit_marker').each(function(el){
    el.hide();
    });

  jQuery('.show_marker').each(function(el){
    el.show();
    }); 
}

jQuery(document).ready(hideEditableMarkers());

I don't know why it does not work.

4 Answers 4

2

The first parameter of the each callback function is the index of the element not the reference to it

so here is the jquery code

function hideEditableMarkers() {
  $('.edit_marker').each(function(idx,el){
    $(el).hide(); // You may use 'this' variable in here as it points to the current element as well
    });

  $('.show_marker').each(function(idx,el){
     $(el).show();
    }); 
}
Sign up to request clarification or add additional context in comments.

Comments

0

This:

jQuery(document).ready(hideEditableMarkers());

should be:

jQuery(document).ready(hideEditableMarkers);

You need to pass the function reference to ready so it's executed as the callback handler for the DOM ready event. What you're currently doing is executing the function immediately (when the elements don't exist), then passing the return from that function (nothing) as the callback for .ready().

Comments

0

use $(this) inside each so it takes the current element... what you have is index and use index as jquery selector el.hide()

try this

function hideEditableMarkers() {
 jQuery('.edit_marker').each(function(el){
   $(this).hide();
 });

 jQuery('.show_marker').each(function(el){
   $(this).show();
  }); 
 }

jQuery(document).ready(function(){
  hideEditableMarkers() ;
});

//or

jQuery(document).ready(hideEditableMarkers);

Comments

0

I believe a function should be reusable:

/*global jQuery */

function toggleMarkers (hideSelector, showSelector) {

    jQuery(hideSelector).each(function () {
        jQuery(this).hide();
    });

    jQuery(showSelector).each(function () {
        jQuery(this).show();
    });

}

jQuery(document).ready(function ($) {
    toggleMarkers('.edit_marker', '.show_marker');
});

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.