1

This might be basic, but this issue takes me hours to figure out and I haven't found any solutions for this. I am using Wordpress 3.5 and I have an external javascript file called general.js in this folder: wp-content/themes/[folder_name]/js. In general.js, I write a function called hideError, basically to hide error label that popped out from my textboxes.

(function($) {
function hideErrorLabel(i) {
    //codes for handling label
}
})(jQuery);

I called it like this:

<span class="component-wrapper" onmouseover="hideErrorLabel(0)">
    <input type="text" name="txtName" size="10" />
    <label for="txtName" class="error">All field must be filled.</label>
</span>

I pass a parameter because these textboxes are array. Strangely, it gives me javascript error "hideErrorLabel is not defined". What went wrong? Please help.

3 Answers 3

2
(function($) {
 /* code */
})(jQuery);

will ensure that the /* code */ you provided gets executed after the DOM has been parsed. However, this will prevent hideErrorLabel from being defined when the parser tries to interpet the onmouseover attribute.

In order to fix this you should use modify your event handlers also in the /* code */ section:

(function($) {
function hideErrorLabel(i) {
    //codes for handling label
}

$('.component-wrapper').on('mouseover',hideErrorlabel);
})(jQuery);
Sign up to request clarification or add additional context in comments.

3 Comments

Note that even in the fixed example, hideErrorLabel is still restricted to the function scope of the IIFE and not accessible in the global scope. This is a good practice though, as you normally don't want (and need) to leak your local functions to the global scope. On another note, you probably want to bind at DOM ready, which can be accomplished by replacing the outer (function($) { ... })(jQuery); with jQuery(function($) { ... });
Good idea though, but how do we handle the parameter that way?
@HenryGunawan: You didn't handle the parameter in your original edit, so I ignored it. Usually you would create a little wrapper: $('.component-wrapper').on('mouseover',function(){hideErrorlabel(0);});. However, I believe that your problem could be solved with CSS, and that's a) much easier and b) works with deactivated JS.
1

Just remove the first and last line of that javascript code. You are hiding your function! Declare it openly in any script tag.

<script type="text/javascript>
function hideErrorLabel(i) {
  //codes for handling label
}
</script>

1 Comment

Thanks for your solution. (function($) { })(jQuery); restricts my function to be used only inside it, so I decided to move hideErrorLabel function outside and it works.
1

There are a couple reasons this could be happening, the safest answer which is not the simplest would be to move the event binding in to the code. You could also use delegation and you shouldn't need an index if you're just looking to scope the function. If you do need the index you could get the location implicitly or use a dataset value to explicitly provide one through PHP.

It sounds like you'd just need something like:

(function($) {
  $(function() {
    $(document).on('mouseover', '.component-wrapper', function(event) {
      var $wrapper = $(event.currentTarget);
      $wrapper.find('.error').hide();
    });
  });
})(jQuery);

But you could adapt it to call external functions or provide extra logic as needed.

1 Comment

You could also replace the two outer functions with just jQuery(function($) { /* run at DOM ready */ });

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.