Solved

object syntax

Posted on 2011-03-09
7
325 Views
Last Modified: 2012-05-11
function testing2(x) {
frm = $(x).closest("form");
alert(frm); /*will give "object Object" which is what is to be expected*/
alert(frm[x].value); /*here i want to alert 12 but syntax doesnt work - what do i need to do?*/
}

<form name="zwxy">
<div onclick="testing2('someelement');">Testing2</div>
<input type="hidden" name="someelement" value="12">
</form>
0
Comment
Question by:dgrafx
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:gavsmith
ID: 35082740
Why not use jquery again?:

alert($(x).val);
0
 
LVL 82

Expert Comment

by:leakim971
ID: 35082786
Use :
frm.get(0)[x].value

Open in new window

or :
frm[0][x].value

Open in new window

instead :
frm[x].value

Open in new window


test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script language="javascript">
	function testing2(x) {
		frm = $("[name='" + x + "']").closest("form");		 
		alert(frm);
		alert(frm[0][x].value);
	}
</script>
</head>
<body>
<form name="zwxy">
<div onclick="testing2('someelement');">Testing2</div>
<input type="hidden" name="someelement" value="12">
</form>
</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35082805
Check the line 9 too (previous code) and this one :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script language="javascript">
	function testing2(obj, x) {
		frm = $(obj).closest("form");		 
		alert(frm);
		alert(frm[0][x].value);
	}
</script>
</head>
<body>
<form name="zwxy">
<div onclick="testing2(this, 'someelement');">Testing2</div>
<input type="hidden" name="someelement" value="12">
</form>
</body>
</html>

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 25

Author Comment

by:dgrafx
ID: 35082964
gavsmith: dont know what you mean?
the snippet you posted alerts a jquery function - the text of the function

leak: I cant ever use form[0] or 1 or ???
you may have more than 1 form on the page ...
0
 
LVL 82

Expert Comment

by:leakim971
ID: 35083028
yes, you use closest : http://api.jquery.com/closest/

Description: Get the first ancestor element that matches the selector, beginning at the current element and progressing up through the DOM tree.

the [0] or get(0) is to get the html object (DOM object) itselft instead the jquery object
0
 
LVL 10

Expert Comment

by:gavsmith
ID: 35083037
it should have been:

alert($(x).val());

however you should be able to use leak's advice you can use frm[0]. There may be more than 1 form on the page but using frm = $(x).closest("form") means frm should only have the one you want.
0
 
LVL 25

Author Comment

by:dgrafx
ID: 35083536
ok - i see what you did - you added another var for the onclick
i posted just onclick="testing2('someelement');"
that threw me a bit cause I didn't notice it.

It works using either way with your [0]
thanks ...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery Scroll on Mobile devices 1 67
JavaScript Scope issue 4 40
Asp.net mvc 5 5 43
Jquery get each value 2 15
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now