Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

Javascript and passing variables to functions

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
zack4x4
Asked:
zack4x4
  • 3
  • 2
1 Solution
 
GowthamNatarajanCommented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
keep in mind, this answer will ONLY WORK if you are using jQuery on your page.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
zack4x4Author Commented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
 
zack4x4Author Commented:
Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now