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
Solved

how to check for existing element before loading variable

Posted on 2011-03-18
2
343 Views
Last Modified: 2012-05-11
Attached code. This works, but there are circumstances where the "selectprogram" radio button may not exist on the page (or there could be many with that name). In that case, my function fails, I believe because this becomes invalid:

var inputVal = $("#mainform input[name='selectprogram']:checked").val();

If there is no element with the name "selectprogram" then I want to do this:

document.mainform.submit();

This is possible? Do I just need to add another conditional statement? Thank you!
function setprogram(displaycat,task){
	document.mainform.displaycat.value=displaycat;
	document.mainform.task.value=task;

	var inputVal = $("#mainform input[name='selectprogram']:checked").val();

	if (undefined==inputVal){
		if ($('#saveandviewbutton').attr("disabled")==true) {
			$('#workoutalert').fadeIn();
		}
	} else {
		document.mainform.submit();
	}
}

Open in new window

0
Comment
Question by:bbdesign
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35168933
$("#mainform input[name='selectprogram']:checked").length return the number of object found.

var $selection = $("#mainform input[name='selectprogram']:checked");
if( $select.length == 1) {
// only one found, phew !!!
}
else if( $select.length >=1 ) {
// more than one found !!!!
}
else {
// ok submit !!
 document.mainform.submit();
}

Open in new window

0
 

Author Comment

by:bbdesign
ID: 35183777
.length -- great! Easy, too. Thanks.
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dynamic Dropdowns 15 32
jQuery - Add confirm message to button? 2 20
IFrame in MVC 2 23
Need help with jQuery elements filtering 2 12
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 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…

860 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