By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

JavaScript coding to swap the content of two text box elements

P: 98
hi everyone....
i want the coding help in Javascript to "swap the content of two Textbox elements" ..Kindly reply me.....
Jul 15 '08 #1
Share this Question
Share on Google+
11 Replies


P: 6
Hi,

Please explain clearly what u want exactly. swap the text in textbox means?
Jul 15 '08 #2

dmjpro
100+
P: 2,476
hi everyone....
i want the coding help in Javascript to "swap the content of two Textbox elements" ..Kindly reply me.....
Expand|Select|Wrap|Line Numbers
  1. <input type=text id=text_id1 value='' />
  2. <input type=text id=text_id2 value='' />
  3.  
Expand|Select|Wrap|Line Numbers
  1. function swap_content(){
  2. var tmp = document.getElementById('text_id1').value;
  3. document.getElementById('text_id1').value = document.getElementById('text_id2').value;
  4. document.getElementById('text_id2').value = tmp;
  5. }
  6.  
Debasis Jana
Jul 15 '08 #3

P: 21
Expand|Select|Wrap|Line Numbers
  1. <input "type="text" name="text_id1" id="text_id1" value='' access here value from the PHP or other,,   eg. echo"$_POST['text_id1']";  "/>
  2. <input type="text" name="text_id1" id="text_id2" value='' access here value from the PHP or other eg. echo"$_POST['text_id2']";  ""//>
  3. <input type="submit" value="swap" onClick="swap_content("text_id1","text_id2");"> 
  4.  
Expand|Select|Wrap|Line Numbers
  1. function swap_content(id1,id2){
  2. var tmp = document.getElementById('text_id1').value;
  3. document.getElementById('text_id1').value = document.getElementById('text_id2').value;
  4. document.getElementById('text_id2').value = tmp;
  5. }
  6.  
I think this will be work good .


dkyadav80
Jul 15 '08 #4

acoder
Expert Mod 15k+
P: 16,027
I think this will be work good .
There are one or two problems: you've got double quotes within double quotes, and you're passing the ids but not making use of them.
Jul 15 '08 #5

P: 98
i want to swap the content of two text box by using javascript...I have written code as follows,but its not working....kindly reply me any one as soon as possible....

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <input "type="text" name="text_id1" id="text_id1" value='a' >
  3. <input type="text" name="text_id1" id="text_id2" value='b' >
  4. <input type="submit" value="swap" onClick=swap_content(text_id1,text_id2)>
  5. <script type="javascript/text">
  6. function swap_content(id1,id2)
  7. {
  8.  var tmp = document.getElementById(text_id1).value;
  9.  document.getElementById(text_id1).value = document.getElementById(text_id2).value;
  10. document.getElementById(text_id2).value = tmp;
  11. }
Jul 17 '08 #6

P: 98
i want to swap the content of two text box by using javascript...I have written code as follows,but its not working....kindly reply me any one as soon as possible....

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <input "type="text" name="text_id1" id="text_id1" value='a' >
  3. <input type="text" name="text_id1" id="text_id2" value='b' >
  4. <input type="submit" value="swap" onClick=swap_content(text_id1,text_id2)>
  5. <script type="javascript/text">
  6. function swap_content(id1,id2)
  7. {
  8. var tmp = document.getElementById(text_id1).value;
  9. document.getElementById(text_id1).value = document.getElementById(text_id2).value;
  10. document.getElementById(text_id2).value = tmp;
  11. }
Jul 17 '08 #7

hsriat
Expert 100+
P: 1,654
[html]<input type="text" name="text_id1" id="text_id1" value="a" >
<input type="text" name="text_id2" id="text_id2" value="b" >
<input type="submit" value="swap" onclick="swap_content('text_id1','text_id2')">
<script type="text/javascript">
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>[/html]

I've underlined the part of the code where you had made some mistakes.
Jul 17 '08 #8

P: 98
Thank U...I got the output....

[html]<input type="text" name="text_id1" id="text_id1" value="a" >
<input type="text" name="text_id2" id="text_id2" value="b" >
<input type="submit" value="swap" onclick="swap_content('text_id1','text_id2')">
<script type="text/javascript">
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>[/html]

I've underlined the part of the code where you had made some mistake.
Jul 17 '08 #9

debasisdas
Expert 5K+
P: 8,127
question moved to javascript forum.
Jul 17 '08 #10

hsriat
Expert 100+
P: 1,654
question moved to javascript forum.
Question already there in JS forum. Both threads just need to be merged.
Jul 17 '08 #11

acoder
Expert Mod 15k+
P: 16,027
sreekandan, as a full member now, you should know that we expect your code to be posted in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use the tags in future.

Also, do not double/triple post your questions. All duplicate threads have been merged.

MODERATOR.
Jul 17 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.