?
Solved

object syntax

Posted on 2011-03-09
7
Medium Priority
?
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…
Suggested Courses

800 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