All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Array Value In jQuery

Last Updated : Mar 11, 2024

How To Get Array Value In jQuery

In this article we will show you the show you the solution of how to get array value in jQuery, here we needs to define array, as we know which is used to store multiple items or when does process with multiple values.

In general we can access any array value using loops. With the help of looping process or specifying array with index value can also get value in particular.

We defined html(), and click() methods for different purpose.

Step By Step Guide On How To Get Array Value In jQuery :-

Here we defined at html block, button element ‘Get Array Value’ which is allow us to process some function and h3 defined for appends result on webpage.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that we waiting for click event occurrence which is appended with button.

There we defined array variable ‘arr’ with few values then we specifically pointed array ‘arr’ third position value by using index value with some string on h3 elements.

<!DOCTYPE html>
<html>
    <head>
        <title>Get array value</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    var arr=["Lilly","Rose","Jamine"];
                        $("h3").html("Got Array Value is "+arr[2]);
                })
            });
        </script>
    <body>
        <button>Get Array Value</button>
        <h3></h3>
    </body>
    </head>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we defined click() method which is appends with button by using tag name ‘button’ for generates click event when user clicks on ‘Get Array Value’ button.
  8. Within function we specified array variable ‘arr’ with ‘"Lilly","Rose","Jamine"’ values. If you want any other values in array or define array with your aspect just modify it accordingly.
  9. Using html() method we can easily appends any text or variable value on webpage and in jquery with same behavior, another method also available that is text() method.
  10. Finally we appended message of ‘Got Array Value is’ with result of 3rd index value in array ‘arr’. The ‘arr[2]’ code will reflect the result on webpage and if you wants to display any other position value then change position.
  11. In html block we defined button element ‘Get Array Value’ and h3 tag for displays any result on webpage.
  12. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get array value in jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see button ‘Get Array Value’ now user needs to clicks on it then it prints third position value with message like ‘Got Array Value is Jamine’.

I hope this article on how to get array value in jQuery helps you and the steps and method mentioned above are easy to follow and implement.