Solved

object syntax

Posted on 2011-03-09
7
329 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 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
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)

690 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