0

Can someone tell me why is the call to my function hideShow not working ?

<a id='plusMinus0' href='#' title='Afficher la liste des messages d erreurs' onclick='hideShow(0);'>

function hideShow(number){
     alert("i am here");
     $('#errMsgGroup'+number).toggle('slow', function() {
         if($('#plusMinus'+number).children('img').attr('src').indexOf('images/NORIA_minus.JPG') >= 0){
                $('#plusMinus'+number).children('img').attr('src', 'images/NORIA_plus.JPG');
                $('#plusMinus'+number).children('img').attr('title', 'hide errors');        
            }
            else{
                $('#plusMinus'+number).children('img').attr('src', 'images/NORIA_minus.JPG');
                $('#plusMinus'+number).children('img').attr('title', 'show errors');
            }
        });
     }
10
  • 2
    Is your JS code inside <script> tags? Commented Jul 2, 2013 at 10:19
  • 1
    Are there any error messages in the Javascript console? Commented Jul 2, 2013 at 10:19
  • 1
    It might be worth posting a more complete version of your html and Javascript. Commented Jul 2, 2013 at 10:20
  • 1
    Still shows the alert for me, even with the new markup: jsfiddle.net/GYM38/1. If you want useful help, create a jsfiddle.net demo with the problem. Commented Jul 2, 2013 at 10:30
  • 1
    Well, the problem in the fiddle is that the function is defined inside the load event handler. That means it cannot be found by the inline event handler. The console shows the error ReferenceError: hideShow is not defined. Change onLoad in the select box to No wrap - in <head>. Since you said earlier that you don't get an error, I wonder if this is really the issue or you just didn't know how to use jsfiddle properly. Commented Jul 2, 2013 at 10:35

5 Answers 5

1

put your function within <script></script> tag

Like

<script>


function hideShow(number){
     alert("i am here");
     $('#errMsgGroup'+number).toggle('slow', function() {
         if($('#plusMinus'+number).children('img').attr('src').indexOf('images/NORIA_minus.JPG') >= 0){
                $('#plusMinus'+number).children('img').attr('src', 'images/NORIA_plus.JPG');
                $('#plusMinus'+number).children('img').attr('title', 'hide errors');        
            }
            else{
                $('#plusMinus'+number).children('img').attr('src', 'images/NORIA_minus.JPG');
                $('#plusMinus'+number).children('img').attr('title', 'show errors');
            }
        });
     }

</script>
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you need to close your A-tag as well like <a id='plusMinus0' href='#' title='Afficher la liste des messages d erreurs' onclick='hideShow(0);'>hide / show</a>

4 Comments

If you think that's the problem than you should also mention that JavaScript code belongs inside <script> tags.
MY FUNCTION IS INSIDE THE SCRIPT TAGS ,AND MY a TAG IS CLOSED AS WELL I JUST DIDNT POST IT.
@user2515601: How are we supposed to know that? Maybe you should post a more complete code example.
@FelixKling Sorry, it was a reply after someone else already mentioned that the function should be within the script blocks. That's why I didn't mention it.
0

In addition to @sonusindhu and @putvande answers. You probably want a return false at the end of your javascript.

function hideShow(number){
  alert("i am here");
  $('#errMsgGroup'+number).toggle('slow', function() {
    if($('#plusMinus'+number).children('img').attr('src').indexOf('images/NORIA_minus.JPG') >= 0){
    $('#plusMinus'+number).children('img').attr('src', 'images/NORIA_plus.JPG');
       $('#plusMinus'+number).children('img').attr('title', 'hide errors');        
    }
    else{
      $('#plusMinus'+number).children('img').attr('src', 'images/NORIA_minus.JPG');
      $('#plusMinus'+number).children('img').attr('title', 'show errors');
    }
  });
  return false;
}

1 Comment

OK GUYS, i found out what was causing the problem. It turned out that i have some parasite characters in my php file, some sort of ^M which i was only able to see with a cat -v, i looked it up on the net and found that by doing a dos2unix command on the files in question u delete all the undesired chars. It worked out for me. THANK YOU ALL FOR YOUR TIME.
0

Try this

<html>
<head>
    <script type="text/javascript">
        function hideShow(number) {
            alert("i am here");
            $('#errMsgGroup' + number).toggle('slow', function () {
                if ($('#plusMinus' + number).children('img').attr('src').indexOf('images/NORIA_minus.JPG') >= 0) {
                    $('#plusMinus' + number).children('img').attr('src', 'images/NORIA_plus.JPG');
                    $('#plusMinus' + number).children('img').attr('title', 'hide errors');
                }
                else {
                    $('#plusMinus' + number).children('img').attr('src', 'images/NORIA_minus.JPG');
                    $('#plusMinus' + number).children('img').attr('title', 'show errors');
                }
            });
        }
    </script>
</head>
<body>
    <a id='plusMinus0' href='#' title='Afficher la liste des messages d erreurs' onclick='hideShow(0);'>Check</a>
</body>


</html>

2 Comments

i am still not getting my alert :/
I already checked it, I get alert. Once see for console errors, or else copy the html I have given into a sample page and run it you will get the alert :)
0

Try this:

<a id='plusMinus0' href='#' onclick="hideShow(0)">Test</a>

<script>
  function hideShow(number) {
     alert(number);
  }
</script>

JSFiddle

EDIT:

I think it's more elegant to handle the event with JQuery:

with JQuery

<a id='plusMinus0' href='#'>Test</a>

<script>
    $(function() {
       $("#plusMinus0").click(function() {
           alert("pass parameter from an attribute " + $(this).attr("id"))
       });
    });
</script>

With # in href, after click your anchor navigates to top of page, to avoid this:

<a id='plusMinus0' href='javascript:;'>Test</a>

or leave the href attribute: Without href

5 Comments

If you use jQuery, then you should remove onclick="hideShow(0)".
I am using the onclick bc i have multiple PLUSes so i need to pass a variable in the function definition.
ok, it's your choice, but you can pass parameters via anchors attributes also.
jsfiddle.net/pv33P/3 can u please tell why this simple test is not working ?
Yes, on the left side change the framework extension "on load"=> no wrap - in head jsfiddle.net/pv33P/6

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.