tags:

views:

53

answers:

2

Hi, at first, sorry about my english. My question is: I have php generated dynamic div, with id and image source.. If user click on dynamic div its swap img source. My script works, but its swaps all div images, but not selected div img source. Where is problem? how I can swap clicked div img source? Can someone, help me please?

My script:

<script> ( function($) { 
  $(document).ready( function() { 
    $("div.area_map").click( function () { 
      $('div[id^=grass]').each(function(div) { 
        $('img.hoverswap').css("width","229"); 
        $('img.hoverswap').css("height","124");
        $('img.hoverswap').attr("src","default/citymap/D5.png"); 
        $('img.hoverswap').css("z-index", "9999") 
       } ); 
     }); 
   } ) ( jQuery ); 
</script>

And div:

<div class="area_map" id="grass
  <?php echo $k+$st_x/2; ?>" 
  style='cursor:pointer; width:118px; height:51px; position:absolute; 
  left:<?php echo $st_x; ?>px; 
  top:<?php echo $st_y; ?>px;'>
  <img src="default/citymap/Zale2.png" name="table" border="0" 
    usemap="#tableMap" class="hoverswap" 
    style='cursor:pointer; z-index:-300; width:118px; height:51px;'/> 
</div>
A: 

use

$(document).ready( function() { 
    $("div.area_map").click( function () { 
                $('img.hoverswap')
                .css(
                {
                "width":"229",
                "height":"124",
                "position":"absolute",//to make z-index work
                "z-index", "9999"
                })
                .attr("src","default/citymap/D5.png");
       }); 
   });

also edit css for div.area_map

div.area_map{
 position:relative;
}

this will make your z-index work relative to area_map

JapanPro
Can you give me your email? I want to send full source code to You.
Nation
just add the codes in http://jsfiddle.net/ , click save and update url here
JapanPro
I cant put php, just html, java and css
Nation
you just add , every one including me can see and change if needed. or you can put your email in jsfiddle so every one can contact.
JapanPro
@user45.. check the updated code
JapanPro
Yes, now its working :) Tnx
Nation
I have another question: when user selected dinamic tab its swaped image. How to save user made changes in SQL or maybe in xml file :)
Nation
if its working voteup and accept as solution. Post a new question as new post; but simple answer will be $.post('sendData.php', function(data) { $('.result').html(data);});
JapanPro
Okey :) I will try
Nation
accept this as solution , increase your acceptance rate before putting new question. the more acceptance rate the more good answer will be here ins stackoverflow.
JapanPro
I cant make it :(
Nation
ya you cant vote coz u dont have point, but you can accept as solution. just click right sign. you will get point after that, after that in future you will be able to vote as well.
JapanPro
A: 

To clarify the answers given by JapanPro and zrytane, $('img.hoverswap') finds all matches in the document, regardless of what scope you call it from.

$('img.hoverswap', this) restricts the scope to this rather than the default of document.

$(this).find('img.hoverswap') sets up a jQuery wrapper for this and then searches within it.

Both work equally well but you'd have to benchmark them yourself to see if there's a speed difference. (Intuitively, there shouldn't be)

However, your script is inefficient since you're repeatedly creating and discarding jQuery objects. Here's how I'd code it:

<script>
 (function($) { 
  $(document).ready( function() { 
    $("div.area_map").click( function () { 
        $('img.hoverswap', this).css({
            width: "229",
            height: "124",
            zIndex: "9999"
        }).attr("src","default/citymap/D5.png"); 
    }); 
  }); 
 }) ( jQuery ); 
</script>

That way, you're only creating the wrapper for the img.hoverswap query once and then sending multiple commands. You'll also notice I've used the map form of .css() which, due to restrictions on the Javascript object literal, uses camelCase rather than dashed-names.

ssokolow
Tnx, its worked. But there is problem with z-index. Swaped D5.png is under grass.png?
Nation
Pull it up in a DOM Inspector (Chrome built-in or Firefox extension) and take a look at the z-index values in the "computed style" listings for both images. That'll tell you where the problem is.
ssokolow
Okey, tnx. I will try.
Nation
It was just position problem. Now its working :)
Nation
I have another question: when user selected dinamic tab its swaped image. How to save user made changes in SQL or maybe in xml file :)
Nation
ssokolow