Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Cannot seem to get javascripts String.indexOf to work ???

    0 down vote favorite
    share [g+] share [fb] share [tw]


    I have a fairly large program that I have simplified to show the problem that I am having. I have 2 div's under which I have an image and some text. When I hover on the image, the jQuery hover over image event fires. Through all this, I am able to get the Id's of both the images & their parent div's. However, when I try to get the index value of the Id's in an array, it always returns -1 ie... not found.

    Can you please help. The code is enclosed below. I am new to javascript & jQuery and hence the need of some help. Maybe the solution is very trivial.

    Thanks in advance. Regards, sbguy

    <html>
    <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var input_array = new Array("input1_Id", "input2_Id");
    var div_array = new Array("div1_Id", "div2_Id");

    window.onload=function(){
    jQuery.ready();
    img_hover_event_handler();
    }
    //________________________________________________

    function img_hover_event_handler() {
    var input_id = "";
    var div_id = "";
    var input_index, div_index;
    $("input.Lrollover").hover(
    function() {
    input_id = new String(this.id);
    input_index = new String(input_array.indexOf(div_id));
    div_id = new String($(this).closest('div').attr('id')); // id of the parent div
    div_index = new String(div_array.indexOf(div_id));
    alert(input_id + ", " + input_index + ", " + div_id + ", " + div_index);
    },
    function() {
    });
    }

    </script>
    </head>

    <body>
    <div id="div1_Id">
    <input class="Lrollover" id="input1_Id" type="image" src="images/LGbtn_off.png" alt="Image1" />
    <p id="myp1_Id">"Hello World 1"</p>
    </div>
    <br><br><br>
    <div id="div2_Id">
    <input class="Lrollover" id="input2_Id" type="image" src="images/LGbtn_off.png" alt="Image2"/>
    <p id="myp2_Id">Hello World 2"</p>
    </div>

    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    new String() is obsolete. If you want to create a string, just assign a string value to a variable

    Examples:
    Code:
    var input_id = this.id;
    Second: In the thread title you are talking about String.indexOf ... but you are only using Array.prototype.indexOf to get the index of an array entry. What is it you intended to do?

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Code:
    input_index = new String(input_array.indexOf(div_id));
    In the above line, where is div_id getting its value from?

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    My suggestion would be to ditch the jquery and use just plain javascript for something relatively basic like this.

    Code:
        <body>
    
            <div id="div1_Id">
                <input onmouseover="getIDs(this)" id="input1_Id" type="image" src="num1.jpg" alt="Image1" />
                <p id="myp1_Id">"Hello World 1"</p>
            </div>
    
            <div id="div2_Id">
                <input onmouseover="getIDs(this)" id="input2_Id" type="image" src="num2.jpg" alt="Image2"/>
                <p id="myp2_Id">Hello World 2"</p>
            </div>
    
            <script type="text/javascript">
                var input_array = ["input1_Id", "input2_Id"];
                var div_array = ["div1_Id", "div2_Id"];
                function getIDs(elem){
                    elemIDidx = input_array.indexOf(elem.id);
                    parentIDidx = div_array.indexOf(elem.parentNode.id);
                    alert(elemIDidx+' '+parentIDidx);
                }
            </script>
    
        </body>
    Last edited by webdev1958; 03-21-2012 at 01:49 PM.


  •  

    Tags for this Thread

    Posting Permissions

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