views:

66

answers:

2

Below is the html code of my problem I need to copy the contents of one textbox to another onblur or onkeyup of similar ID. Kindly help me. The javascript function "sync" should be updated for my issue. Thanks in advance.

<html>

</head>
    <body>
        <div id="container">        

            <script type="text/javascript">
function sync(order){
    var val, i, val1;
    val = document.getElementsByTagName('input');
    alert(val.length);
    for (i=0;i<val.length;i++){
            if(val[i].getAttribute('id')==order){
                val1 = document.getElementById(order).value;
                if(val1 != '')
                    break;
            }

    }

}

</script>
            <div id="body">
                <div id="wrapper">
                    <form id="assignship" name="assignship" action="/drivewise-dst/assignship" method="post">
<table class="wwFormTable">
    <table width="1024" border="0" cellspacing="0" cellpadding="0">

            <tr>
                <td height="10" colspan="3" class="titlebar"
                    style="background-repeat: no-repeat; align: right">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="3">
                <table width="1011" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="17">&nbsp;</td>
                        <td width="10" bgcolor="#CCCCCC" class="header">&nbsp;</td>
                        <td width="110" bgcolor="#CCCCCC" class="header">Export To Excel</td>
                        <td width="130" bgcolor="#CCCCCC" class="header">Order #</td>
                        <td width="175" bgcolor="#CCCCCC" class="header">First Name</td>
                        <td width="205" bgcolor="#CCCCCC" class="header">Last Name</td>
                        <td width="142" bgcolor="#CCCCCC" class="header">Tracking #</td>
                        <td width="115" bgcolor="#CCCCCC" class="header">Serial #</td>
                    </tr>
                </table>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                <div
                    style="width: 993px; height: 350px; overflow: auto; overflow-x: hidden; background-color: #f9f5e3;">
                <table width="1024" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td colspan="7">
                        <div align="center"></div>
                        </td>
                    </tr>
                    <tr>
                        <td width="114">
                        <div align="center"><input name="Export7" type="checkbox"
                            id="Export7" onclick="javascript:checkedAll();" /></div>
                        </td>
                        <td width="105">&nbsp;</td>
                        <td width="175">&nbsp;</td>
                        <td width="175">&nbsp;</td>
                        <td width="142">&nbsp;</td>
                        <td width="142">&nbsp;</td>
                    </tr>


                        <input type="hidden" name="vinHidden" value="VIN" id="assignship_vinHidden"/>
                        <tr>
                            <td width="114" align="center"><input
                                value="0"
                                type=checkbox name="chkbox" id="chkbox"></input></td>
                            <td width="105" class="chartcopy">1067</td>
                            <td width="175" class="chartcopy">ELEPHANT</td>
                            <td width="175" class="chartcopy">FOX</td>
                            <td width="142"><input type="text" name="trackingNumber" size="15" maxlength="50" value="" id="1067" onblur="javascript:sync('1067')"/></td>
                            <td width="160">
                            <div class="action-error"></div>
                            <input type="text" name="serial" size="15" maxlength="10" value="" id="serial"/>
                            </td>
                        </tr>

                        <input type="hidden" name="vinHidden" value="VIN" id="assignship_vinHidden"/>
                        <tr>
                            <td width="114" align="center"><input
                                value="1"
                                type=checkbox name="chkbox" id="chkbox"></input></td>
                            <td width="105" class="chartcopy">1067</td>
                            <td width="175" class="chartcopy">ELEPHANT</td>
                            <td width="175" class="chartcopy">FOX</td>
                            <td width="142"><input type="text" name="trackingNumber" size="15" maxlength="50" value="" id="1067" onblur="javascript:sync('1067')"/></td>
                            <td width="160">
                            <div class="action-error"></div>
                            <input type="text" name="serial" size="15" maxlength="10" value="" id="serial"/>
                            </td>
                        </tr>

                        <input type="hidden" name="vinHidden" value="VIN" id="assignship_vinHidden"/>
                        <tr>
                            <td width="114" align="center"><input
                                value="2"
                                type=checkbox name="chkbox" id="chkbox"></input></td>
                            <td width="105" class="chartcopy">1067</td>
                            <td width="175" class="chartcopy">ELEPHANT</td>
                            <td width="175" class="chartcopy">FOX</td>
                            <td width="142"><input type="text" name="trackingNumber" size="15" maxlength="50" value="" id="1067" onblur="javascript:sync('1067')"/></td>
                            <td width="160">
                            <div class="action-error"></div>
                            <input type="text" name="serial" size="15" maxlength="10" value="" id="serial"/>
                            </td>
                        </tr>

                        <input type="hidden" name="vinHidden" value="VIN" id="assignship_vinHidden"/>
                        <tr>
                            <td width="114" align="center"><input
                                value="3"
                                type=checkbox name="chkbox" id="chkbox"></input></td>
                            <td width="105" class="chartcopy">1085</td>
                            <td width="175" class="chartcopy">CAT</td>
                            <td width="175" class="chartcopy">DOG</td>
                            <td width="142"><input type="text" name="trackingNumber" size="15" maxlength="50" value="" id="1085" onblur="javascript:sync('1085')"/></td>
                            <td width="160">
                            <div class="action-error"></div>
                            <input type="text" name="serial" size="15" maxlength="10" value="" id="serial"/>
                            </td>
                        </tr>

                        <input type="hidden" name="vinHidden" value="VIN" id="assignship_vinHidden"/>
                        <tr>
                            <td width="114" align="center"><input
                                value="4"
                                type=checkbox name="chkbox" id="chkbox"></input></td>
                            <td width="105" class="chartcopy">3333</td>
                            <td width="175" class="chartcopy">APPLE</td>
                            <td width="175" class="chartcopy">BOY</td>
                            <td width="142"><input type="text" name="trackingNumber" size="15" maxlength="50" value="" id="3333" onblur="javascript:sync('3333')"/></td>
                            <td width="160">
                            <div class="action-error"></div>
                            <input type="text" name="serial" size="15" maxlength="10" value="" id="serial"/>
                            </td>
                        </tr>

                        <input type="hidden" name="vinHidden" value="VIN" id="assignship_vinHidden"/>
                        <tr>
                            <td width="114" align="center"><input
                                value="5"
                                type=checkbox name="chkbox" id="chkbox"></input></td>
                            <td width="105" class="chartcopy">3333</td>
                            <td width="175" class="chartcopy">APPLE</td>
                            <td width="175" class="chartcopy">BOY</td>
                            <td width="142"><input type="text" name="trackingNumber" size="15" maxlength="50" value="" id="3333" onblur="javascript:sync('3333')"/></td>
                            <td width="160">
                            <div class="action-error"></div>
                            <input type="text" name="serial" size="15" maxlength="10" value="" id="serial"/>
                            </td>
                        </tr>

                        <input type="hidden" name="vinHidden" value="VIN" id="assignship_vinHidden"/>
                        <tr>
                            <td width="114" align="center"><input
                                value="6"
                                type=checkbox name="chkbox" id="chkbox"></input></td>
                            <td width="105" class="chartcopy">3334</td>
                            <td width="175" class="chartcopy">APPLE</td>
                            <td width="175" class="chartcopy">BOY</td>
                            <td width="142"><input type="text" name="trackingNumber" size="15" maxlength="50" value="" id="3334" onblur="javascript:sync('3334')"/></td>
                            <td width="160">
                            <div class="action-error"></div>
                            <input type="text" name="serial" size="15" maxlength="10" value="" id="serial"/>
                            </td>
                        </tr>

                        <input type="hidden" name="vinHidden" value="VIN" id="assignship_vinHidden"/>
                        <tr>
                            <td width="114" align="center"><input
                                value="7"
                                type=checkbox name="chkbox" id="chkbox"></input></td>
                            <td width="105" class="chartcopy">3335</td>
                            <td width="175" class="chartcopy">APPLE</td>
                            <td width="175" class="chartcopy">BOY</td>
                            <td width="142"><input type="text" name="trackingNumber" size="15" maxlength="50" value="" id="3335" onblur="javascript:sync('3335')"/></td>
                            <td width="160">
                            <div class="action-error"></div>
                            <input type="text" name="serial" size="15" maxlength="10" value="" id="serial"/>
                            </td>
                        </tr>

                </table>
                </div>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td colspan="3">
                <table width="1011" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td height="43" bgcolor="#FFFFFF">&nbsp;</td>
</td>
                        <td width="157" bgcolor="#FFFFFF"><input type="button" alt="" id="xxx" name="xxx" value="submit"/>

                        </td>
                    </tr>
                </table>
                </td>
            </tr>


    </table>
</table></form>                                     
                </div>
             </div>
        </div>
    </body>

</html>
+3  A: 

Your main issue is all your <input> elements have the same id, "1067". Not only is this not XHTML compliant, but it will cause issues with your javascript, as document.getElementById(order) will return multiple elements, a scenario which this function is not designed to handle.

I'd suggest applying classes to all your <input> elements, use jQuery to bind the .blur() event to each element of that class, and perform your logic that way (use jQuery's foreach enumerator rather than a for loop with id matching).

Of course you could accomplish this with regular JavaScript too (as you have done, inline "onblur" event wiring on the actual elements).

But first things first, fix up the HTML so it can play nice with JavaScript.

RPM1984
Actually my intention is copy the same text of trackingNum entered in first textbox to other same OrderID.... if possible can you please help me with step wise changes and javascript code using jQuery.
Colours
Its not easy to answer - as your HTML makes no sense. Is all your HTML fixed? Or is a dynamically generated? It looks like you have a bunch of tr's with the same content inside. Is this dynamic content, or are you manually typing all this HTML in? If manual, get rid of all the ID's, and instead, just wrap each TD in a DIV with an ID. Then you can get elements by div id, e.g ('#sometr .trackingnumber') will give you the element with a class of tracking number inside a unique DIV. You could apply classes to elements you want to get a handle of - and work that way.
RPM1984
+1  A: 

id is supposed to be unique, you should not have more than one element with the same id on a page, and you cannot use getElementById to reference them (it will probably return the first element in document order with the id).

You can do it...if you must, by using getElementsByTagName and a loop (somewhat similar to your code)

function getIds(id) {
  var inputs = document.getElementsByTagName('input'), matches = [];
  for (var i=0; i<inputs.length; i++) 
    if (inputs[i].getAttribute('id') == id) matches.push(inputs[i]);

  return matches;
}

But you should really re-factor and put "similar" values in element classes, in which case, the code above will still work, just replace 'id' with 'class'

MooGoo
I have changed "id" of each text box to class and modified the getAttribute('class') in above method, but not sure how to proceed further in copying the data from similar text boxes. Please help me proceed further.
Colours
Well, the function `getIds` returns an array of elements, so you would want to loop through that array and set the `value` property of each item to whatever data you are copying.
MooGoo