2

I have an array of strings, I want to show each array element in its own <div> tag like <div "class">one</div><div class"two">one</div> and each div tag should have a class that is common for all. All process start on click button

BUTTON CODE

<asp:Button OnClientClick="abc();" runat="server" />

JAVASCRIPT FUNCTION

function abc()
    {
        debugger;
        var arrayVariable = "one,two,three";
        var arrayLength = arrayVariable.length;
        var temp;
        for (i = 0; i < arrayLength; i++) {
            temp = document.createElement('div');
            temp.className = 'results';
            temp.innerHTML = arrayVariable[i];
            $('#inputcomshow').append(temp);
            //document.getElementById("#inputcomshow").value = replaced
        }

        }
9
  • Just stating what you want or need, is not a proper question for this site. You need to describe to us what specifically you are having trouble with. Please go read How to Ask. Commented May 31, 2018 at 6:37
  • Arrays are defined using []. So define your array var arrayVariable =[ 'one','two','three']; Commented May 31, 2018 at 6:38
  • @sudhir ojha i admitted array formate is [ 'one','two','three'] ok but i am getting the same problem Commented May 31, 2018 at 6:52
  • @CBroe i wand to show these elements like one,two,three show in <div class="classname"><div class="a">one<div><div class="a">two<div><div> formate Commented May 31, 2018 at 6:53
  • 1
    @CBroe I'll make my meaning clearer: There was no need for the sarcastic tone in "Did I not just tell you, that...". A bit of patience can help a beginner go a long way. Commented May 31, 2018 at 7:41

3 Answers 3

4

You need to convert string into array first.
Use this:

   function abc()
    {
        debugger;
        var stringVariable = "one,two,three"; // string
        var arrayVariable = stringVariable.split(","); // now string to array
        var arrayLength = arrayVariable.length;
        var temp;
        for (i = 0; i < arrayLength; i++) {
            temp = document.createElement('div');
            temp.className = 'results';
            temp.innerHTML = arrayVariable[i];
            $('#inputcomshow').append(temp);
            //document.getElementById("#inputcomshow").value = replaced
        }
     return false;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thankx for help but i i have already try it but the problem is its not stable i mean it shows when click on button for a second then automatically hide it??
@aparnarai do you see appended html on page, may be it be hide by some css
ok so can you please do one more answer about this question? how to add one more <span class=""></span> with this div> like <div id= "inputcomshow"><div class="results" ><span class="res" >x<span></div></div>
@aparnarai you can just update as temp.innerHTML = arrayVariable[i]+'<span class="res" >x</span>';
1

Use This

<asp:Button OnClientClick=" javascript:return abc();" runat="server" />



    <script>
    function abc() {
        debugger;
        var stringVariable = "one,two,three"; // string
        var arrayVariable = stringVariable.split(","); // now string to array
        var arrayLength = arrayVariable.length;
        var temp;
        for (i = 0; i < arrayLength; i++) {
            temp = document.createElement('div');
            temp.className = 'results';
            temp.innerHTML = arrayVariable[i];
            $('#inputcomshow').append(temp);
            //document.getElementById("#inputcomshow").value = replaced
        }
        return false;
    } 
</script>

1 Comment

Thank you for Answer can you please do one more answer about this question? how to add one more <span> with this div
0

Try it with less number variable and pure javascript, because you only used single line jQuery code $('#inputcomshow').append(temp);.

function abc(){
    var arrayVariable = "one,two,three";
    arrayVariable = arrayVariable.split(',');
    for (i = 0; i < arrayVariable.length; i++) {
        temp = document.createElement('div');
        temp.className = 'results';
        temp.innerHTML = arrayVariable[i];
        document.getElementById("inputcomshow").appendChild(temp);
    }
}

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.