Solved

Javascript and passing variables to functions

Posted on 2014-04-11
6
340 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
Comment Utility
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 32

Expert Comment

by:Big Monty
Comment Utility
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 32

Expert Comment

by:Big Monty
Comment Utility
keep in mind, this answer will ONLY WORK if you are using jQuery on your page.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Author Comment

by:zack4x4
Comment Utility
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 32

Expert Comment

by:Big Monty
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
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)

772 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

9 Experts available now in Live!

Get 1:1 Help Now