IF in JQuery

Posted on 2008-11-07
Last Modified: 2012-05-05
Hello Experts!

I have a function:

                  $("a.NALink", this).click(function(){
                  var confirmNA = confirm("You are about to delete last year's answer(s). Are you sure you want to delete this information?");
                  return confirmNA

It DOES NOT work as it's supposed to.

It SHOULD check if there's element ("form[name*='Form1']") EXISTS on the Page.

If it does, AND user clicks the link $("a.NALink", this).click ONLY THEN

the Confirm-Cancel Pop-up should appear.

NOW it works always and not checking if element  ("form[name*='Form1']") EXISTS.

Should I add "If" statement to the function and if yes, what will be the syntax for that?

Question by:snarinsky
    LVL 19

    Accepted Solution

    Your getting the link within the context of the form...

    What happens if you use  $("form[name*='Form1']").find("a.NALink")

    Expert Comment

    use if($("form[name*='Form1']").length > 1)  to check if the form exists or not

               if($("form[name*='Form1']").length > 1) {
                      $(this+" > a.NALink", this).click(function(){
                      var confirmNA = confirm("You are about to delete last year's answer(s). Are you sure you want to delete this information?");
                      return confirmNA

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
    DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery ( 1.6 introduces .prop() ( and .removeProp() ( methods which allow modifying or removi…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now