On Sat, Jul 11, 2009 at 8:24 PM, jt<[email protected]> wrote:
>
>
> I have only used jQuery for fairly simple things and am a little
> stumped on how best to achieve the following. I have a list of items
> (similar to the three items at the bottom) and I want a user to be
> able to assign it as a favorite. I just copied the .post part from the
> docs with the id in html comments but have two related questions:
> 1. what is the best way / preferred to get the id of value of the item
> in the list (ie structured into the html - pretty sure an id can't be
> just an integer)?
> 2. assuming I wanted to change the class back after a succesful update
> (to say for example is_favorited), what would be a possible way?
>
> $(function(){
>   $('.favorite').click(function(){
>      //alert('you want to save');
>      //alert(this);
>      $.post("test.php", {id: 'how do i get this'},
>        function(data){
>          alert("Data Loaded: " + data);
>        });
>    });
> });
>
>
> <span class='favorite'>something</span><br/><!-- 1 -->
> <span class='favorite'>somethone</span><br/><!-- 2 -->
> <span class='favorite'>something other</span><!-- 3 -->
>

<span class="favorite" id="favorite_x">...

You can either parse out the integer in JS or pass the entire
"favorite_x" string to the PHP script to parse it out there.

$(function(){
        $('.favorite').click(function()
        {
                var fave_id = $(this).attr('id').substr(9);
                $.post(
                        "test.php",
                        {id: fave_id},
                        function(data)
                        {
                                alert("Data Loaded: " + data);
                        }
                );
        });
});

$(function(){
        $('.favorite').click(function()
        {
                $.post(
                        "test.php",
                        {id: $(this).attr('id')},
                        function(data)
                        {
                                alert("Data Loaded: " + data);
                        }
                );
        });
});

Reply via email to