Solved

Javascript using 2 scripts on 1 field

Posted on 2012-03-17
4
145 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

749 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