www.webdeveloper.com
Results 1 to 4 of 4

Thread: Get the index of an element contained into an array, according to the event object

  1. #1
    Join Date
    Jan 2013
    Posts
    84

    Get the index of an element contained into an array, according to the event object

    Hi, ive this code

    html:
    HTML Code:
    <div class="contStella" id="stellaquattro"><img class="stellaVota" src="immagini_sito/stella.png"/></div>
    Javascript + Jquery

    PHP Code:

    var stelleArray=new Array ( $('#stellauno'), $('#stelladue'), $('#stellatre'), $('#stellaquattro'), $('#stellacinque') )

    $(
    '#stellaquattro').mouseover(illuminastelle)
       
    function 
    illuminastelle(event
    {
       
    alert(stelleArray.indexOf(event.target)) // <-this retrive me "-1" that means that the element hasn't been found in the array

    what's the solution? The event.target is "stellaquattro" right? and for what reason i can't found it into the array?
    I tryied event.currenttarget too, without success
    Last edited by American horizo; 07-04-2013 at 12:02 PM.

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    try this

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>123</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    var stelleArray=['stellauno','stelladue','stellatre','stellaquattro','stellacinque'];
    function illuminastelle(event){
    var id=$(this).attr('id');
    alert(id+'; '+stelleArray.indexOf(id));
    }
    $(document).ready(function(){
    $('#stellaquattro').mouseover(illuminastelle);
    });
    </script>
    </head>
    <body>
    <div class="contStella" id="stellaquattro"><img class="stellaVota" src="immagini_sito/stella.png" width="100" height="100" /></div>
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Jan 2013
    Posts
    84
    Yes i thought to this solution, but is it possibile to refers directly do an html element considering tat the variable/array contains a direct reference to it?

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,267
    The jQuery objects are not the DOM objects !
    Try an alert(JSON.stringify($('#stellauno'))) to see this jQueryobject. Replace this object by $('#stellauno').get(0) to get the DOM object.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles