Click here to Skip to main content
16,021,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
JavaScript
function copy_data(val){
if(document.getElementById(question1).value == ''){
 var a = document.getElementById(val.id).value
 document.getElementById("question1").value=a
 }
 var quest2 =document.getElementById(question2).value;
 if(quest2.value == ''){

 document.getElementById("question2").value=a
 }
 var quest3 = document.getElementById(question3).value;
 if(quest3.value == ''){
 document.getElementById("question3").value=a
 }
 }

HTML
<input type="text" name="q3" id="q3" value="What is your nick name?" style="font-size:9px" height="10" onclick="copy_data(this)"/>
 
<input type="text" name="q6" id="q6" value="How old are you?" style="font-size:9px;" height="10" onclick="copy_data(this)"/>
 
<input type="text" name="q4" id="q4" value="What is your birth date?" style="font-size:9px" height="10" onclick="copy_data(this)"/>
 


<input id="question1" name="question1" type="text"/>
 <input id="question2" name="question2" type="text"/>
 <input id="question3" name="question3" type="text"/>


In my code by clicking each question text move into the particular textbox.
if question1 is not empty text move it to question2 textbox .


help anyone

thank you..
Posted
Updated 15-Aug-15 21:29pm
v2
Comments
ZurdoDev 11-Aug-15 7:33am    
Where exactly are you stuck?

There are some problems with the code you currently have:

  1. Sometimes there are no quotes around the IDs inside getElementById.
  2. quest2 and quest3 already contain the values of the question2 and question3 elements, so you cannot get .value from it a second time.
  3. If you fix the problems above, there's still this problem remaining: clicking on one box will fill all empty text boxes with the same question. This is undesirable, so you should first check val.id to see which text box you should change.

This code snippet should fix the above problems:
JavaScript
function copy_data(val) {
    var quest1 = document.getElementById("question1");
    var quest2 = document.getElementById("question2");
    var quest3 = document.getElementById("question3");
    if (quest1.value == '' && val.id == 'q3') {
        quest1.value = val.value;
    } else if (quest2.value == '' && val.id == 'q6') {
        quest2.value = val.value;
    } else if (quest3.value == '' && val.id == 'q4') {
        quest3.value = val.value;
    }
}
 
Share this answer
 
Hope the below snippet helps

JavaScript
function copy_data(val)
{
	var a = document.getElementById(val.id).value;	
	var quest1 = document.getElementById("question1").value;
	var quest2 = document.getElementById("question2").value;
	var quest3 = document.getElementById("question3").value;
	if(quest1 == "")
	{
		document.getElementById("question1").value=a;
	}
	if (quest2 == "")
	{
		document.getElementById("question2").value=a;	
	}
	if (quest3 == "")
	{
		document.getElementById("question3").value=a;	
	}
}
 
Share this answer
 
Hi
Please find the solutions for this using jquery
1. download min.js file from jquery website add reference to your project.


XML
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <script src="Scripts/jquery-1.8.2.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {
            $(".abc").live("click", function () {
                var selectedQuestion = "";
                var $this = $(this);
                if ($this.is('input')) {
                    selectedQuestion += $this.val();
                    var id = $this.closest('td').next().find('input').attr("id");
                    $('#' + id).val(selectedQuestion);
                }
            });
        });


    </script>
    <title></title>
</head>
<body>

    <table>
        <tbody>
            <tr>
                <td>
                    <input type="text" name="q3" id="q3" class="abc" value="What is your nick name?" style="font-size:9px" height="10" />
                </td>
                <td>
                    <input id="question1" name="question1" type="text"/>
                </td>
            </tr>

            <tr>
                <td>
                    <input type="text" name="q6" id="q6" class="abc" value="How old are you?" style="font-size:9px;" height="10" />
                </td>
                <td>
                    <input id="question2" name="question2" type="text" />
                </td>
            </tr>

            <tr>
                <td><input type="text" name="q4" id="q4" value="What is your birth date?"  class="abc" style="font-size:9px" height="10" /></td>
                <td><input id="question3" name="question3" type="text" /></td>
            </tr>
        </tbody>
    </table>
<div></div>
</body>
</html>



thanks
sandeep miriyala
vanisandeep@gmail.com
Let me know any query you have mail me above.
 
Share this answer
 
XML
 <script type="text/javascript">
        $(document).ready(function () {
            $(".abc").live("click", function () {
                if ($(this).is('input'))
                    $('#' + $(this).closest('td').next().find('input').attr("id")).val($(this).val());
            });
            $("#Clear").click(function () {
                $('.clear').val('');
            });
        });
    </script>
    <title></title>
</head>
<body>

    <table>
        <tbody>
            <tr>
                <td>
                    <input type="text" name="q3" id="q3" class="abc" value="What is your nick name?" style="font-size:9px" height="10" />
                </td>
                <td>
                    <input id="question1" name="question1" type="text" class="clear" />
                </td>
            </tr>

            <tr>
                <td>
                    <input type="text" name="q6" id="q6" class="abc" value="How old are you?" style="font-size:9px;" height="10" />
                </td>
                <td>
                    <input id="question2" name="question2" type="text" class="clear" />
                </td>
            </tr>

            <tr>
                <td><input type="text" name="q4" id="q4" value="What is your birth date?"  class="abc" style="font-size:9px" height="10" /></td>
                <td><input id="question3" name="question3" type="text" class="clear" /></td>
            </tr>
            <tr>
                <td>
                    <input type="button" id="Clear" value="Clear/Reset" />
                </td>
            </tr>
        </tbody>
    </table>
 
Share this answer
 
Comments
CHill60 19-Aug-15 9:16am    
In what way is this different to the solution you posted 10 minutes earlier? If you have something to add please use the Improve solution link. Posting multiple solutions can only confuse the OP and other visitors

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900