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

Javascript and passing variables to functions

Posted on 2014-04-11
6
347 Views
Last Modified: 2014-04-11
Hi All,

   I've never had to do this (not very familiar with JS) but I'm trying to pass multiple variables through to a function in Javascript.  This is what I have but it's not working...

In the header <script>
function yesno(a,b,c,d,e,f) {
	var yes = a;
	var yesbgcol = b;
	var yesfgcol = c;
	var pts = d;
	var answer = e;
	var elem = f;

	if (document.getElementById($elem).value == answer) {
		document.getElementById($elem).innerHTML = pts;
		document.getElementById($elem).style.backgroundColor=yesbgcol;
		document.getElementById($elem).style.color=yesfgcol;
	} else {
		document.getElementById($elem).innerHTML = "";
		document.getElementById($elem).style.backgroundColor='white';
		document.getElementById($elem).style.color='black';
	}
}

Open in new window


What I have in the HTML

<input type="text" id="flightdutylimitation" name="flightdutylimitation" required Pattern="[yYnN]" size=10 maxlength=1 onBlur="yesno('Y','red','white','FNA','Y','aflightdutylimitation');">

Open in new window


Thanks!
0
Comment
Question by:zack4x4
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
GowthamNatarajan earned 500 total points
ID: 39994230
Please try this..
If using document.getElementById then dont use $
If you wanna change innerhtml of any other div, pass its id as 6th argument

<input type="text" id="flightdutylimitation" name="flightdutylimitation" required Pattern="[yYnN]" size=10 maxlength=1 onBlur="yesno('Y','red','white','FNA','Y','flightdutylimitation');">


function yesno(a, b, c, d, e, f) {
           
      var yes = a;
      var yesbgcol = b;
      var yesfgcol = c;
      var pts = d;
      var answer = e;
      var elem = f;

          if (document.getElementById(elem).value == answer) {
          
                document.getElementById(elem).innerHTML = pts;
                document.getElementById(elem).style.backgroundColor=yesbgcol;
                document.getElementById(elem).style.color=yesfgcol;
          } else {
          
                document.getElementById(elem).innerHTML = "";
                document.getElementById(elem).style.backgroundColor='white';
                document.getElementById(elem).style.color='black';
          }
    }
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39994476
you're mixing javascript and jquery it seems. wherever you have

document.getElementById($elem)

will fail, you're better off using the jquery syntax (a lot easier to read and produces shorter code. also, lines 2-7 in your function are not really needed, you can just define those variable names directly in your function definition. Here's how I would rewrite your function:

function yesno( yes, yesbgcol, yesfgcol, pts, answer, elem ) {

	if ( $elem).val() == answer) {
		$(elem).html( pts );
		$(elem).attr( 'backgroundcolor', yesbgcol );
		$(elem).attr( 'color', yesfgcol );
	} else {
		$(elem).html();
		$(elem).attr( 'backgroundcolor', 'white' );
		$(elem).attr( 'color', 'black' );
	}
}

Open in new window

0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39994477
keep in mind, this answer will ONLY WORK if you are using jQuery on your page.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Author Comment

by:zack4x4
ID: 39994515
Sorry Big - I was thinking half in PHP with the $ haha.  I tried to learn jquery but I don't have a solid base of javascript to start learning anything new.  It's so hard to try and keep up with all the changes. :(
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39994535
no problem, I understand, it's tough keeping up with all of the technologies out there :) if that's the case, GowthamNatarajan's solution should work.
0
 
LVL 4

Author Closing Comment

by:zack4x4
ID: 39994740
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

856 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