Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript using 2 scripts on 1 field

Posted on 2012-03-17
4
Medium Priority
?
149 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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

715 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