Solved

Javascript using 2 scripts on 1 field

Posted on 2012-03-17
4
147 Views
Last Modified: 2013-02-03
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.
0
Comment
Question by:kkbenj
[X]
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
  • 2
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
nap0leon earned 500 total points
ID: 37732628
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
 

Author Comment

by:kkbenj
ID: 37732650
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
 
LVL 18

Expert Comment

by:nap0leon
ID: 37732704
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
 

Author Comment

by:kkbenj
ID: 37734579
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

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.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

632 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