Solved

object syntax

Posted on 2011-03-09
7
324 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
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…

757 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

23 Experts available now in Live!

Get 1:1 Help Now