Solved

Javascript using 2 scripts on 1 field

Posted on 2012-03-17
4
140 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now