Link to home
Start Free TrialLog in
Avatar of snarinsky
snarinskyFlag for United States of America

asked on

If - Else and setting Sesion Variable in Jquery

Hi Experts!

I'm new to JQuery, so could you please help me out with the following:

I have an ASP page. This page has a dynamically generated <Form> element.

The <name> of this Form element can be different <Q22Form1>, <Q4Form1>, <Q30 Form1>, etc. But <name> attribute always contains ...Form1>. Unfortunately, it does NOT have an ID.

E.g:

<FORM action="../.../05/answer_proc05.asp" method=POST name="Question5Form1">


1). I need to check if the element with <name> containing "Form1" exists on the page.

2). If it's there i need to set some kind of a session variable indicating that this element exists on the page.

I think I would want to use some JQuery selector, but don't know the exact syntax for "If" condition and for setting Session variable inside JQuery function.

Please help!
Thank you.
Avatar of hielo
hielo
Flag of Wallis and Futuna image

The syntax you seek is basically this:
$("form[name*='Form1']").each(function(){alert($(this).name)});

>>some kind of a session variable
The problem is that the session needs to be set on the server BEFORE you send the page. jQuery/javascript runs on the browser AFTER you have the server has sent the data/output.
1) $("form[name*='Form1']") matches form element where attribute name contains (*=) 'Form1'
(see http://docs.jquery.com/Selectors)


2) to set a server side session, you have to have a server side page in where the session gets set and call it via jquery's ajax method

$.ajax({ url: <yoururl>, async: false }).
Setting async to false makes the ajax call synchronous and javascript waits until the call is completed.
(see http://docs.jquery.com/Ajax)
Avatar of snarinsky

ASKER

Thank you hielo!

AlbertVanHalen, do I need any additional plugins for doing that?
No, just jquery...
Sorry for being such a dummy :) But...

This works just fine, except for 1 thing - alert is showing up with "undefined". Isn't it supposed to display the name of the form?

<script type="text/javascript">
        $(document).ready(function()
        {
         $("form[name*='Form1']").each(function(){alert($(this).name)});
         });
 </script>

where do i nsert the $.ajax({ url: <yoururl>, async: false }) ?

And where does the change of Session variable go?

Thanks!
$("form[name*='Form1']").each(function(){
  $.ajax({url:"yourpage.asp",params:"formname=" + encodeURIComponent($(this).name) });
 
 }
);


yourpage.asp
If Trim(Request("formname")) <> "" Then
 Session("name")=value
End If
ASKER CERTIFIED SOLUTION
Avatar of Albert Van Halen
Albert Van Halen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you all guys!

Our approach to this problem changed a little though. Instead of using Session Variables, i need to do the following:

IF there is an element with name containing "From1" on a page THEN Look for link <a...></a> AND if link <a....></a> was clicked - then show users a Confirm-Cancel pop-up before redirecting to this link.

With your help I have this function working:

<script type="text/javascript">
        $(document).ready(function()
        {
         $("form[name*='Form1']").each(function(){alert($(this).attr("name"))});
         });
 </script>

Now it shows the pop up for every element with name containing "...Form1...".
How do I make it find the link I need ONLY if there's an element with name containing "...Form1..." and show the Confirm-Cancel popup in this case?

Should be fairly simple..

THANK YOU!

To look for a specific element you could use the elements name, otherwise any element will be (in jquery) a *. So $("*[id*='Form1']").
To further look for a link (does it have specific attributes like a className or an id ?)
To jquery for classnames you use . $("a.myClass")
To use it for a specific id use # $("#myID")

So to look for a specific link within another element whose ID contains Form1 and bind a function to its click event use $("*[id*='Form1'] a.myClass").bind("click", myFunction)