An alternative?....

$(document).ready(function(){
  var txtLinks = $('a'); // change selector to suit
  var txts = { newText: ["My new text A","My new text B","My new text
C","My new text D","My new text E","My new text F"]
                 , oldText: $.map(txtLinks, function(v,i){ return $
(v).text(); })
                 };
  $('input').click(function(){ // change selector to suit
      var mid = this.id;
      txtLinks.each(function(i){ $(this).text( txts[mid][i] ); });
    });
});


On Dec 1, 11:47 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> Hi Mali,
>
> Try this instead:
> I'm still fairly new to jQuery (1 week), so I'm sure there must be an
> even shorter way...
>
> $(function() {
>
>         $("#newText").click(function(){
>
>                 $('#myList a').each(function(x){
>                         if (!this.origText) {
>                                 this.origText = $(this).text();
>                         }
>
>                         arr=["My new text A","My new text B","My new text 
> C","My new text
> D","My new text E","My new text F"];
>
>                         $(this).text(arr[x]);
>                 });
>
>         });
>
>         $("#oldText").click(function(){
>
>                 $('#myList a').each(function(x){
>                         $(this).text(this.origText);
>                         //$('#dump').html(printProps(this, 'obj'));
>
>                 });
>
>         });
>
> });
>
> Regards
> Lee
>
> Mali wrote:
> > Hello,
> > I am a webdesigner who is new to javascript and this is my first week
> > of learning jquery. I enjoy it very much.  I have a question about
> > array.
>
> > What I am trying to do is to translate the text of each links into
> > other language by using array.  I want the texts to be switched when
> > clicking "Give me new texts/ Give me old texts" buttons.
>
> > What I did below is a straightforward way by selecting each <a> tag
> > directly and replace it with the new texts. Also, I would like to
> > 'clone' the original text so that I can switch it back.
>
> > I am sure that there is a better way of doing this using ".each",
> > ".clone" and array technique.
> > The problem is that I don't know how to combine all these in the
> > logical way.
>
> > Can someone give me a good direction please. Your answer will be
> > greatly appreciated
>
> > Here is my code
>
> > -------------------------------------------------------------------
>
> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
> >                     "http://www.w3.org/TR/html4/loose.dtd";>
> > <html>
> > <head>
> > <script src="http://code.jquery.com/jquery-latest.js";></script>
> > <script language="javascript">
>
> > $(function() {
> > arr=["My new text A","My new text B","My new text C","My new text
> > D","My new text E","My new text F"];
> > $("#newText").click(function(){
> > $("#myList a:eq(0)").text(arr[0]);
> > $("#myList a:eq(1)").text(arr[1]);
> > $("#myList a:eq(2)").text(arr[2]);
> > $("#myList a:eq(3)").text(arr[3]);
> > $("#myList a:eq(4)").text(arr[4]);
> > $("#myList a:eq(5)").text(arr[5]);
> > });
>
> > });
>
> > </script>
>
> > </head>
> > <body>
>
> > <input id="newText" type="button" value="Give me new texts" />
> > <input id="oldText" type="button" value="Give me old texts" />
>
> > <ul id="myList">
> >    <li><a href="#">Text A</a></li>
> >    <li><a href="#">Text B</a></li>
> >    <li><a href="#">Text C</a></li>
> >    <li><a href="#">Text D</a></li>
> >    <li><a href="#">Text E</a></li>
> >    <li><a href="#">Text F</a></li>
> > </ul>
>
> > </body>
> > </html>
>
> > -----------------------------------------------------------------------------

Reply via email to