Welcome Guest, Not a member yet? Register   Sign In
problem with the confirmation before delete using javascript
#1

[eluser]alberta[/eluser]
hello,

I have problem using javascripting code in codeigniter.
i have to delete an entity in table. for this i've written this code .

I have js file named confirm.js

Code:
<js>
$(document).ready(function(){
$(".confirmClick").click( function() {
    alert("hello");
    if ($(this).attr('title')) {
        var question = 'Are you sure you want to ' + $(this).attr('title').toLowerCase() + '?';
    } else {
        var question = 'Are you sure you want to do this action?';
    }
    if ( confirm( question ) ) {
        [removed].href = this.src;
    } else {
        return false;
    }
});
});
</js>

then in my view file i've added this line

Code:
<a href="projects/projects_delete/{id}" class="confirmClick" title="Delete this project">Delete</a>

in header file i've also added this

[removed][removed]

but when i click on delete, it doesn't ask for confirmation and it redirects to next page just Sad
Can some one tell me where is the mistake or i've omitted some step.

Thanks in advance
#2

[eluser]Frank Berger[/eluser]
try this:

Code:
<js>
$(document).ready(function() {
  $('.confirmClick').live('click',function() {
    var q = 'Are you sure you want to do this action?';
    var t = $(this).attr('title');
    if (t.length > 0) q = 'Are you sure you want to ' + t.toLowerCase() + '?';
    var c = confirm(q);
    return c;
  });
});
<js>

what happens is that confirm returns true or false, and you return that to the click-event. If its true, the click will be executed (no need for a window-location-href), if it is false the click will be aborted.

If you don't use (for whatever reason) jQuery 1.3+, then replace the .live('click',function with .click(function . the live method ensures that even if the link is added to the content after the event has been bound that it will apply to this link (like if the link has been loaded through ajax or been added with jquery in any other way.

Additionally, I always let the confirm return into a variable, I had weird race-conditions before when I had the confirm inside an if-statement + it is not really needed (the if-statement) in this case.

The title is read into a var first, because your statement would be true otherwise even if the title is empty (like <a href="bla" title="">xx</a> ) this way it is checked for real content.

I wrote above from my head, but i am pretty sure it'll work

cheers
Frank
#3

[eluser]alberta[/eluser]
I have even tried this code. but problem is nothing is displayed. Is there some step missing to enable javascript ? because it doesn't prompt for confirmation. That means javascript code is not run. Have i missed some code in enabling js?
#4

[eluser]Dam1an[/eluser]
Can I see the line where you include the confirm.js file?
You might be doing it using a relative path, which breaks once you add some URI segments
#5

[eluser]Frank Berger[/eluser]
what happens if you put that in your page (into the head section of your html code preferably):
Code:
<sc-ript type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></sc-ript>
<sc-ript type="text/javascript">
$(document).ready(function() {
alert('bing');
});
</sc-ript>

of course, you write 'sc-ript' as 'script'

Frank
#6

[eluser]alberta[/eluser]
In header i've written this line

Code:
<sc-ript type='text/javascript' src='{site_url}theme/js/confirm.js'></sc-ript>

@ Frank , when i write your code then there is alert box saying bing. but no alert box appears when i click on delete lilnk.
#7

[eluser]Frank Berger[/eluser]
I am sorry, but this works for me out of the box:
Code:
&lt;html&gt;
    &lt;head&gt;
        <sc-ript type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></sc-ript>
        <sc-ript type="text/javascript">
            $(document).ready(function() {
                $('.confirmClick').live('click',function() {
                    var q = 'Are you sure you want to do this action?';
                    var t = $(this).attr('title');
                    if (t.length > 0) q = 'Are you sure you want to ' + t.toLowerCase() + '?';
                    var c = confirm(q);
                    return c;
                });
            });
        </sc-ript>
    &lt;/head&gt;
    &lt;body&gt;
        <a href="delete.html" title="delete this" class="confirmClick">Click me to execute the action</a>
    &lt;/body&gt;
&lt;/html&gt;

does it work for you if you copy & paste it into an empty file, replace 'sc-ript' with 'script', save, and open the file in the webbrowser as you'd expect it? If yes, then check your markup in your views or where-ever you have it. If no, then use a diffrent Browser (sorry out of options then)
#8

[eluser]Frank Berger[/eluser]
[quote author="alberta" date="1243203962"]In header i've written this line

Code:
<sc-ript type='text/javascript' src='{site_url}theme/js/confirm.js'></sc-ript>

@ Frank , when i write your code then there is alert box saying bing. but no alert box appears when i click on delete lilnk.[/quote]

didn't see the markup before, do you feed the view the correct value for your {site_url} variable? what do you see there when you open the source-view in your browser viewing the page in question?

Frank
#9

[eluser]alberta[/eluser]
Hey Frank it works fine when i place code in header file as u said. but when i use this
<sc-ript type='text/javascript' src='{site_url}theme/js/confirm.js'></sc-ript> and place all that code in confirm.js file then it doesn't work. Can you tell what's wrong with that.

Well thanks a lot for your help. It finally worked out Smile
#10

[eluser]proxisoft.net[/eluser]
Sorry, resolved!




Theme © iAndrew 2016 - Forum software by © MyBB