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.
snarinskyAsked:
Who is Participating?
 
Albert Van HalenConnect With a Mentor Analyst developerCommented:
Alert is showing undefined because you're referencing jquery's object and that object has no name property.

Instead do alert(this.name) or do alert($(this).attr("name"));

Follow hielo's steps to do the post to your page and set the session server side.
0
 
hieloCommented:
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.
0
 
Albert Van HalenAnalyst developerCommented:
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)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
snarinskyAuthor Commented:
Thank you hielo!

AlbertVanHalen, do I need any additional plugins for doing that?
0
 
Albert Van HalenAnalyst developerCommented:
No, just jquery...
0
 
snarinskyAuthor Commented:
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!
0
 
hieloCommented:
$("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
0
 
snarinskyAuthor Commented:
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!

0
 
Albert Van HalenAnalyst developerCommented:
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)
0
All Courses

From novice to tech pro — start learning today.