0

I am using ajax GET to recieve some html response body. Once i got the response body i parsed out the desired strings. In my case the desired strings are names of popular american basketball teams. Then i stored the parsed strings in a string array called teamNames.

So if i were to print out the string array teamNames it would print names like => Heat, Raptors, Pacers etc.

Now i want to use some ajax method to create button names for all the strings sotred in the teamNames array.

The following is my javascript.

  <script>
    $( document ).ready(function()
    {
       $("#log").click(function(){

     $.ajax({
        url: "http://1xx.1xx.0.1xx:8081/script.login",
            type: "GET",
            data: { 'page':'create_user', 'access':'user','username':'user', 'password':'user'},
            dataType: "text",
            success: function (html) {
                //Prints the Response Body
                console.log(html);


          var teamNames = new Array();
          var res = html.match(/insertNewChild(.*);/g);
                for(var i =0;i<res.length;i++)
                {

                    //Parse out names of the desired strings         
                    var temp = res[i].split(',');
                    if(temp.length >= 3)
                    {

               //Store the desired strings in an string array 
              teamNames[i] = (temp[2].replace('");','')).replace('"','');
                    }                   
                }


              for(var i = 0; i<teamNames.length; i++){

                 //this will print names of all the basketball teams (desired string)
                 alert(widgetNames[i]);

                 //create buttons using the printed string


              }

            }

       });
      });


    })

</script>

In my html code i have a div called item which is in some navigation menu.

  <div id="items">
 <!-- Should be loaded using some ajax method -->
 <a href="" data-theme="b" data-role="button">Raptors</a>
  <a href="" data-theme="b" data-role="button">Heat</a>
 <a href="" data-theme="b" data-role="button">Pacers</a>
<a href="" data-theme="b" data-role="button">Celtics</a>
<a href="" data-theme="b" data-role="button">Bulls</a>
<a href="" data-theme="b" data-role="button">Orlando</a>

 <!-- Should load by default. -->
<a href="#page1" data-transition="fade" data-theme="b" data-icon="flat-cross" data-role="button">LOG OUT</a>
  </div>

I would like to be able to create the buttons listed above dynamical using ajax . So for instance if my teamNames array contains Raptors then i want to dynamically create the following inside the items div. ==>

<a href="" data-theme="b" data-role="button">Raptors</a>

Please advice on how this can be done? I apologize again if this is a bad question as i am a js and html noob. i have asked a couple of questions in the past here and got great help that got me started. Any tip or hint is appreciated.

1
  • 2
    You haven't explained about the problem you are facing. You mentioned only "What you want".. Commented Jan 27, 2014 at 18:02

3 Answers 3

1

You can use the following code"

for(var i = 0; i<teamNames.length; i++){
   $('#items').prepend('<a href="javascript:;" data-theme="b" data-role="button" >'+teamNames[i]+'</a>');
}
Sign up to request clarification or add additional context in comments.

3 Comments

or append, depending what he really wants
Although all the codes are working fine this one i happened to use first. THANKS so much. i dont have enough reps to give all of you a upvote.
@hrehman , just a small problem for some reason it works but my data-theme="b" is not working. I see no color for the created buttons. any idea why?
0

I won't show how to process the buttons because I'm not sure what you're trying to do with them, but I will show you how to create them.

var buttons = new Array();

for(var i = 0; i < teamNames.length; i++){

    //this will print names of all the basketball teams (desired string)
    alert(widgetNames[i]);

    //create buttons using the printed string
    buttons[i] = $("<a href='#' data-theme='b' data-role='button'>" + widgetNames[i] + "</a>");       

}

That should populate an array with one button-style anchor for each value in widgetNames.

Comments

0

Shortest way

No Loop needed !!

var buttonsHTML='<a href="#" data-theme="b" data-role="button">'+
teamNames.join('</a><a href="#" data-theme="b" data-role="button">')+
'</a>';

or

var 
a='<a href="#" data-theme="b" data-role="button">',
b='</a>',
btns=a+teamNames.join(b+a)+b;

Demo

http://jsfiddle.net/7Nu5k/2/

If you have any questions just ask!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.