SunnyD
SunnyD

Reputation: 280

onclick not calling javascript function

I'm having a simple issue with calling a javascript function. I've been playing with this for hours and I can't see the problem. Hopefully another perspective can help.

in the of my php file:

<script type="text/javascript">
function showShareDiv(objid){
    var div = document.getElementById('share'+objid);
    if (div.style.display=='none'){
        div.style.display='block';
    }
    else{
        div.style.display='none');
    }
}
</script>

This is just to show/hide a div with name "share"+number (eg. share104). When I look at the source the $obj->id correctly names the div and function onclick name.

Here is the button:

<div id="sharebutton" style="width:100%;" onclick="showShareDiv('<?=$obj->id?>');">
  <center>Share</center>
</div>
<div id="share<?=$obj->id?>" style="display:none;">
  SHARE BUTTONS GO HERE
</div>

Any help is appreciated.

Upvotes: 1

Views: 2957

Answers (2)

Adrian B
Adrian B

Reputation: 1631

You can put an alert('test') in function, to see if function is called.

Upvotes: 0

Naftali
Naftali

Reputation: 146350

You have an extra )

function showShareDiv(objid){
    var div = document.getElementById('share'+objid);
    if (div.style.display=='none'){
        div.style.display='block';
    }
    else{
        div.style.display='none'; //had a `)` here
    }
}

Now it works: http://jsfiddle.net/maniator/eHMZR/

Upvotes: 5

Related Questions