Solved

object syntax

Posted on 2011-03-09
7
327 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

829 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