views:

60

answers:

2

I've got code like this on my page:

<div class="item item_wall id1"></div>
<div class="item item_wall id2"></div>
<div class="item item_wall id3"></div>
<div class="item item_wall id4"></div>
<div class="item item_wall id5"></div>

I want something in jquery so that when a div with the class of "item" is clicked, it will load another div from a page, the page's filename will depend on the div clicked. Preferably i would like it to load:

something.php?type=item_wall&id=id1

For future reference this is what I ended up with:

<div id="itemstable" class="item_love">
    <div class="id1"></div>
    <div class="id2"></div>
    <div class="id3"></div>
    <div class="id4"></div>
    <div class="id5"></div>
    <div class="id6"></div>
</div>

jQuery:

<script>
$("#itemstable div").click(function(){
    var url = "something.php?type=" + $(this).parent().attr("class") + "id=" + $(this).attr("class");
    alert(url);
}); 
</script>
+1  A: 

Echoing Spencer Ruport's answer, you can make this easier when using jQuery to use custom attributes other than class.

For example, your DIV may look like this:

<div class="item" type="item_wall" id="1"></div>

You can easily create jQuery selector and event like the following:

$(".item").click(function() {
    var url = "something.php?type=" + $(this).attr("type");
    windows.location = url;
});

Of course, don't forget to check for validity, never trust user input (your HTML can be rewrote by a malicious user).

Adrian Godong
if I add custom attributes, wouldn't that mean my code wont validate? also you cant apply css to them so i would need to repeat type and id, all 3 classes get some css applied to them. item = width and height, type = which file to use as background, id = the background position so it uses the right image.
Hintswen
If you insist on valid XHTML document, then you can stick with your class declaration, and use string manipulation to parse out your "variables". Or you can create multiple child DIV/SPAN with display:none but contains the value (ala XML). Either way, it's just more work to be done.
Adrian Godong
aah right, well i guess I don't need my code to validate, just thought it would be nice...What about the css issue? Any way I could do it without repeating the type and id?
Hintswen
See my other answer. Still, don't mix data with UI.
Adrian Godong
+2  A: 

For XHTML valid document, the easiest is to add invisible child DIV under your main DIV. E.g.:

<div class="item">
  <div class="variable type">item_wall</div>
  <div class="variable id">1</div>
</div>

You can find the "custom attribute" using .find(). E.g.:

$(".item").click(function() {
    var type = $(this).find(".type").val();
    var url = "something.php?type=" + type;
    windows.location = url;
});

A bit of opinion: mixing data with UI should be avoided.

Adrian Godong
Thanks for all the help/code I think i'll go with your other solution and just include the type and id twice (once in the class, other time as custom attributes). I''m trying to keep my code as small as possible, thats why I'm not going for this option. I actually just thought of applying the "item" and "item_wall" classes to the parent div instead, saving me a lot of space in my code, then I'll use css to set the width of the child divs. Never tried this before but I assume it will work...
Hintswen