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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
snarinskyAuthor Commented:
Thank you hielo!

AlbertVanHalen, do I need any additional plugins for doing that?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
Albert Van HalenAnalyst 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.