Swap Text Input Values w/ Javascript

I know sooo very little about Javascript. I've seen a bunch of examples, but can't figure them out.

Please help make this work with a button and show how to do it with an image instead of  the swap button. There are 2 functions below. There should only be one.

Thanks.
<head>
<script type="text/javascript">

function swap(el) { 
 if (document.getElementById(el)) { 
 var obj = document.getElementById(el); 
 obj.innerhtml=(obj.innerhtml=='A')?'B':'A'; 
 } 
 return false; 
}

function innerHTMLSwap(elm1,elm2)
	{
	var elm1_html = elm1.innerHTML;
	elm1.innerHTML = elm2.innerHTML;
	elm2.innerHTML = elm1_html;
	}

</script>
</head>
<body>
<form class="form" method="post" action="submit_me.php">
	<input class="input" id="depart_box" type="text" name="dep" value="<?php ($dept) ? print $dept : print 'Dep' ?>" size="4" />
	
<input type="button" value="<>" onclick="innerHTMLSwap(document.getElementById('depart_box'),document.getElementById('arrive'))" />
	
	<input class="input" id="arrive_box" type="text" name="arr" value="<?php ($arrv) ? print $arrv : print 'Arr' ?>" size="4" />
	<label for="date"> </label><input class="input" type="text" name="date" value="<?php echo date("n\/j")?>" size="4" />
	<input type="hidden" name="mloads" value="good" />	
		<select name="time">
			<option value ="6A">6 am</option>
			<option value ="9A" selected="selected">9 am</option>
			<option value ="12P">Noon</option>
			<option value ="3P">3 pm</option>
			<option value ="6P">6 pm</option>
			<option value ="9P">9 pm</option>
		</select> 
	<input class="btn" type="submit" value="View" />
</form>
</body>

Open in new window

LVL 1
dolan2goAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
sorry about that. You need to swap the ".value", NOT the ".innerHTML":

function innerHTMLSwap(elm1,elm2)
{
        var elm1_html = elm1.value;
        elm1.value = elm2.value;
        elm2.value = elm1_html;
return false;
}

also, your arrive input has id="arrive_box" NOT id="arrive":
<input type="image" src='image.gif' value="<>" onclick="return innerHTMLSwap(document.getElementById('depart_box'),document.getElementById('arrive_box')) && false;" />
0
 
hieloCommented:
on the code below, you need to change this:
<input type="image" src='IMAGENAME'

so that IMAGENAME is the path AND name to your image.
<head>
<script type="text/javascript">

function innerHTMLSwap(elm1,elm2)
{
        var elm1_html = elm1.innerHTML;
        elm1.innerHTML = elm2.innerHTML;
        elm2.innerHTML = elm1_html;
return false;
}

</script>
</head>
<body>
<form class="form" method="post" action="submit_me.php">
        <input class="input" id="depart_box" type="text" name="dep" value="<?php ($dept) ? print $dept : print 'Dep' ?>" size="4" />
        
<input type="image" src='image.gif' value="<>" onclick="return innerHTMLSwap(document.getElementById('depart_box'),document.getElementById('arrive')) && false;" />
        
        <input class="input" id="arrive_box" type="text" name="arr" value="<?php ($arrv) ? print $arrv : print 'Arr' ?>" size="4" />
        <label for="date"> </label><input class="input" type="text" name="date" value="<?php echo date("n\/j")?>" size="4" />
        <input type="hidden" name="mloads" value="good" />      
                <select name="time">
                        <option value ="6A">6 am</option>
                        <option value ="9A" selected="selected">9 am</option>
                        <option value ="12P">Noon</option>
                        <option value ="3P">3 pm</option>
                        <option value ="6P">6 pm</option>
                        <option value ="9P">9 pm</option>
                </select> 
        <input class="btn" type="submit" value="View" />
</form>
</body>

Open in new window

0
 
dolan2goAuthor Commented:
OK. I tried that. The image is being displayed, but when it's clicked, the form's action is being submitted. It doesn't matter about the '&& false;' at the end. On the first attempt, I didn't notice the 'return' slipped in. After adding that, clicking the link does nothing.

Is the function really correct?

Only looking at the pertinent pieces of the code (the 2 text inputs, script and input  with js call), here's how the code sits:
	<input class="input" id="depart" type="text" name="dep" value="<?php ($dept) ? print $dept : print 'Dep' ?>" size="4" />
	
<script type="text/javascript">
function innerHTMLSwap(elm1,elm2)
{
        var elm1_html = elm1.innerHTML;
        elm1.innerHTML = elm2.innerHTML;
        elm2.innerHTML = elm1_html;
return false;
}
</script>
...

<input type="image" class="image" src="images/arrow_switch.gif" value="<>" return onclick="innerHTMLSwap(document.getElementById('depart'),document.getElementById('arrive'))" />
	
	<input class="input" id="arrive" type="text" name="arr" value="<?php ($arrv) ? print $arrv : print 'Arr' ?>" size="4" />

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dolan2goAuthor Commented:
Great! That did it.

While waiting for your suggestion, I tried the code below. It worked, BUT changed the vaiues back in a second. So, it changed the values twice with one click. Can you see why?

Thanks.

<script>
function swap_content ( id1,id2 ) {
	var tmp = document.getElementById (id1).value;
	document.getElementById (id1).value = document.getElementById (id2).value;
	document.getElementById (id2).value = tmp;
}

</script>

<a href"javascript  onClick="swap_content('dep','arr')"><img src="images/arrow_switch.gif" alt="Swap Cities"></a>

Open in new window

0
 
hieloCommented:
you need to return false in order to "cancel" the link's default behaviour (which is to navigate). Most likely the click swaps the value but then it must be "refreshing" the page causing it to swap back to the original. Also, the href should not have the onclick within it. Onclick should be by itself as shown below:

<a href="#"  onClick="return swap_content('dep','arr')"><img src="images/arrow_switch.gif" alt="Swap Cities"></a>
notice also that you missed the "=" sign after href
0
 
kadabaConnect With a Mentor Commented:
to be clear...No points for me....

<a href="#"  onClick="return swap_content('dep','arr');return false;"><img src="images/arrow_switch.gif" alt="Swap Cities"></a>

or

<a href="javascript:void(0);"  onClick="return swap_content('dep','arr')"><img src="images/arrow_switch.gif" alt="Swap Cities"></a>
0
 
dolan2goAuthor Commented:
Great help. Thanks very much for taking time on a Friday night to answer this 'un-ending' question.

I appreciate it, because learning javascript and AJAX will have to wait for another day and assistance like this is indespensible.

The question's over now. Please take the points.

David
0
All Courses

From novice to tech pro — start learning today.