2

I want to Show/Hide image by jQuery by clicking on same <div>.

I can't swap the image with toggle function. I am using it with HTML. this is not working

<script type="text/javascript">

    $(document).ready(function () {
        $("cell").click(function () {
            $('#img1').toggle("slow");


        })
    });
</script>
<div id="gridbox">
        <div class="cell">
        <div class="inner">
            <span class="name">Bob</span> <br /> (id: 57)
          <%--  <input type="hidden" class="friend_id" value="57 " />--%>
            <img src="Images/mobile.jpg" alt="" id="img1" width="180" height="180"/>
        </div>
    </div>
kkk
  • 273
  • 1
  • 6
  • 20
  • Hi, maybe you want to include your code chunk here. and how are you showing and hiding the image? are you using show http://api.jquery.com/show/ or hide http://api.jquery.com/hide/ – melaos Sep 13 '12 at 11:24
  • I answered your question(I think), but what did you mean in the last sentence? – gdoron Sep 13 '12 at 11:27
  • @Roko. Suprising. I thought with two cats and a chimpanzee. :) – gdoron Sep 13 '12 at 11:36

5 Answers5

4
$('#divId').click(function(){
    $('#imgId').toggle();
});
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • just telling that i am using jQuery in html because sometimes user want to know in which you are using jQuery – kkk Sep 13 '12 at 11:30
  • 1
    @undefined & Roko. Hi guys, I'm on a break... :) I just needed to [ask a question](http://stackoverflow.com/questions/12404136/), so in the meanwhile I answered this...; – gdoron Sep 13 '12 at 11:34
  • – kkk Sep 13 '12 at 11:39
  • @kkk. You didn't close the script tag. try make a [demo here](http://jsfiddle.net/) – gdoron Sep 13 '12 at 11:41
2

use .cell instead of cell in ur first line

Hardik Fefar
  • 301
  • 4
  • 19
1

you can use the jquery toogle api

html

<div id="clickhere ">
  Click here
</div>
<img id="image" src="demo.png" alt="" width="120" height="120" />

jquery

$('#clickhere ').click(function() {
  $('#image').toggle('slow', function() {
    // Animation complete.
  });

});

for further reference check this link of jquery .toggle()

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

If by using it with HTML you mean to say you are writing script inline then stop. This has so many negatives and I won't list them all to save time.

Abstract the javascript to a separate file and only use inline to initialise the script. Here's an example of abstracting scripts with classes (uses mootools) http://digitarald.de/project/autocompleter/

Here's the example you should be following: http://jqueryui.com/demos/toggle/

<script>
    $(function() {
        // run the currently selected effect
        function runEffect() {
            // get effect type from 
            var selectedEffect = $( "#effectTypes" ).val();

            // most effect types need no options passed by default
            var options = {};
            // some effects have required parameters
            if ( selectedEffect === "scale" ) {
                options = { percent: 0 };
            } else if ( selectedEffect === "size" ) {
                options = { to: { width: 200, height: 60 } };
            }

            // run the effect
            $( "#effect" ).toggle( selectedEffect, options, 500 );
        };

        // set effect from select menu value
        $( "#button" ).click(function() {
            runEffect();
            return false;
        });
    });
</script>

<div class="demo">

<div class="toggler">
    <div id="effect" class="ui-widget-content ui-corner-all">
        <h3 class="ui-widget-header ui-corner-all">Toggle</h3>
        <p>
            Etiam libero neque, luctus a, eleifend nec, semper at, lorem. Sed pede. Nulla lorem metus, adipiscing ut, luctus sed, hendrerit vitae, mi.
        </p>
    </div>
</div>

<select name="effects" id="effectTypes">
    <option value="blind">Blind</option>
    <option value="bounce">Bounce</option>
    <option value="clip">Clip</option>
    <option value="drop">Drop</option>
    <option value="explode">Explode</option>
    <option value="fold">Fold</option>
    <option value="highlight">Highlight</option>
    <option value="puff">Puff</option>
    <option value="pulsate">Pulsate</option>
    <option value="scale">Scale</option>
    <option value="shake">Shake</option>
    <option value="size">Size</option>
    <option value="slide">Slide</option>
</select>

<a href="#" id="button" class="ui-state-default ui-corner-all">Run Effect</a>
</div>
3rdLion
  • 159
  • 2
0

see this Fiddle

$("div").click(function () {
      $("img").toggle("slow");
}); ​
Sender
  • 6,660
  • 12
  • 47
  • 66