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');
}
});
}
5 Answers 5
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>
Comments
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
<script> tags.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
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
Try this:
<a id='plusMinus0' href='#' onclick="hideShow(0)">Test</a>
<script>
function hideShow(number) {
alert(number);
}
</script>
EDIT:
I think it's more elegant to handle the event 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
onclick="hideShow(0)".
<script>tags?alertfor me, even with the new markup: jsfiddle.net/GYM38/1. If you want useful help, create a jsfiddle.net demo with the problem.ReferenceError: hideShow is not defined. ChangeonLoadin the select box toNo 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.