Welcome Guest, Not a member yet? Register   Sign In
jQuery + AJAX + CI
#1

[eluser]Erik Larsson[/eluser]
Hello!
Im trying to build a AJAX request with jQuery and CI.
the function should work like this.
I have a list of words like this.
Code:
<ul id="searchword_ul">
             <li id="searchword_kuken">
                 &lt;?=form_open('adminpanel/testformremove')?&gt;
                 <div style="display:none">
                     &lt;?=form_input('sokord_id', '3', 'id="sokord_id"')?&gt;
                     &lt;?=form_input('register_id', '1', 'id="register_id"')?&gt;
                     &lt;?=form_input('sokordet', 'kuken', 'id="sokordet"')?&gt;
                 </div>
                 kuken <a href="" id="removeSearchword">x</a>
                &lt;?=form_close()?&gt;
            
             </li>
             <li id="searchword_test">
                 &lt;?=form_open('adminpanel/testformremove')?&gt;
                 <div style="display:none">
                     &lt;?=form_input('sokord_id', '3', 'id="sokord_id"')?&gt;
                     &lt;?=form_input('register_id', '1', 'id="register_id"')?&gt;
                     &lt;?=form_input('sokordet', 'test', 'id="sokordet"')?&gt;
                 </div>
                 test <a href="" id="removeSearchword">x</a>
                 &lt;?=form_close()?&gt;
             </li>
         </ul>
      </div>

and a Javascript like this
Code:
$("#removeSearchword").click(function(){
                var sokord_id = $("#sokord_id").val();
                var register_id = $("#register_id").val();
                var sokordet = $("#sokordet").val();
                
                
                $.post("&lt;?=base_url()?&gt;index.php/adminpanel/testformremove",
                   { sokord_id: sokord_id, register_id: register_id },
                       function(data){
                            $("#searchword_"+ sokordet).slideUp("normal", function() {

                            $("#searchword_"+ sokordet).before('');
                        });
                    }
                );
                return false;
        });

The problem im having, is that i can only press the "x" (remove) link on the first one, not on the next one. Nothing happens when i press the secound "x".

Is CI creating this?

Best
Erik
#2

[eluser]LuckyFella73[/eluser]
maybe it's because you took the same value for the id attributes "removeSearchword"
ids must be unique
#3

[eluser]Erik Larsson[/eluser]
Thank you for the reply!
Need like a dynamic function for that then, becuse it can be diffrent amout of words for diffrent profiles.

Do you know any good solution for that?


// Erik
#4

[eluser]evilgeoff[/eluser]
Well don't use ID's for this use the class identifier instead in jQuery so make it '$(.removeSearchword)' instead of pointing at the id $(#removeSearchword) as it's only going to work for the first one. This way you can iterate through them using different jQuery functions. You can then also dynamically change the class if it suits you using something like this

$(this).removeClass('removeSearchword');
$(this).addClass('newSearchword');

The way you're doing it will work unless there is a duplicate so remove that as something to worry about and assign it a class instead.
#5

[eluser]Erik Larsson[/eluser]
Thank you evilgeoff for your reply!

It helped alot !! Thank you very much Smile




Theme © iAndrew 2016 - Forum software by © MyBB