Javascript using 2 scripts on 1 field

I need a script to blank out the value of a text box onfocus.  That works correctly.  But because that causes "a change", the onchange function is being called.  What is the syntax for onchange to initiate only if this.value isn't null?

HTML:
<input size='3' type='text' name='OtherAmt'  value='Other' class='AddIt' onfocus='otherbox(this)' onchange='additionalSwap(this.name)' />

Open in new window


otherbox():
	function otherbox(what) {
		what.value='';
		what.focus();
	}

Open in new window


I do not want to change the additionalSwap() function.
kkbenjAsked:
Who is Participating?
 
nap0leonConnect With a Mentor Commented:
Try removing the "focus".  I blank out default text during focus and blur events and never have this issue.

With that change, I think your second question is moot.
If not, you cannot stop onchange from firing when a change is detected.  You can wrap the function call with test parameters though...
e.g.,
onchange="if (!(this.value=='')){addtionalSwap(this.name)}"

Open in new window

0
 
kkbenjAuthor Commented:
I have tried with and without the focus.  Behavior doesn't change.  And, unfortunately, the if statement doesn't provide what I need.

It's only FF with the issue.
0
 
nap0leonCommented:
I made an HTML page with only the code you provided and an ad-hoc additionalSwap function that alerts when it fires.  There are no issues.  Something else in your page is causing your problem. (Tested in Firefox 10.0.2)

<input size='3' type='text' name='OtherAmt'  value='Other' class='AddIt' onfocus='otherbox(this)' onchange='additionalSwap(this.name)' />
                                  
<script>
	function otherbox(what) {
		what.value='';
		what.focus();
	}
         

	function additionalSwap(name) {
		alert(name)
	}
</script>

Open in new window

0
 
kkbenjAuthor Commented:
nap0lean - you are correct.  it has nothing to do with the onchange javascript.

It seems the HTML/CSS wrapping around it is the issue.  Let me backup to explain further.  There are a row of radio buttons and the final button is an "Other" box where user can enter free form $ amount.  

The problem is that the focus reverts to the radio button (in FF) when I want it to remain in the text input box.  The box contains the word 'Other' and is cleared with this updated code (works fine):

	function otherbox(what) {
		if (what.value=='Other') {
			what.value='';
		}
	}

Open in new window


and here's the whole section:
<div class='donation-amount'>
  <fieldset>
	<legend>Please choose your monthly donation amount...</legend>
	<label>
		<?php
			$sDetails = "<input name='RB".$SelectedPartnerId."' type='radio' value='19' class='AddIt' onclick='additionalSwap(this.name)' />$19";
			echo $sDetails;
		?>
	</label>
	<label>
		<?php
			$sDetails = "<input name='RB".$SelectedPartnerId."' type='radio' value='29' class='AddIt' onclick='additionalSwap(this.name)' />$29";
			echo $sDetails;
		?>
	</label>
	<label>
		<?php
			$sDetails = "<input name='RB".$SelectedPartnerId."' type='radio' value='49' class='AddIt' onclick='additionalSwap(this.name)' />$49";
			echo $sDetails;
		?>
	</label>
	<label>
		<?php
			$sDetails = "<input name='RB".$SelectedPartnerId."' type='radio' value='79' class='AddIt' onclick='additionalSwap(this.name)' />$79";
			echo $sDetails;
		?>
	</label>
	<label>
		<?php
			$sDetails = "<input name='RB".$SelectedPartnerId."' type='radio' value='99' class='AddIt' onclick='additionalSwap(this.name)' />$99";
			echo $sDetails;
		?>
	</label>
	<label>
	  <span>
		<?php
			$sDetails = "<input name='RB".$SelectedPartnerId."' type='radio' value='Hidden' class='AddIt' />";
			echo $sDetails;
		?>
		$
		<?php
			$sDetails = "<input size='3' type='text' name='OtherAmt".$SelectedPartnerId."'  value='Other' class='AddIt' onfocus='otherbox(this)' />";
			echo $sDetails;
		?>
	  </span>
	</label>
   </fieldset>
</div>

Open in new window


And here is the CSS:
body #main .donation-amount fieldset label input
{
    vertical-align: baseline;
}
body #main .donation-amount fieldset span
{
    padding: 0px 15px;
    display: inline-block;
    border: 1px solid #bfebf5;*/
}
body #main .donation-amount fieldset span input
{
    width: 65px;
}

Open in new window


The functionality works in Chrome and IE, but in FF I cannot enter anything in the box as the focus reverts/remains on the radio button.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.