...

View Full Version : Cannot seem to get javascripts String.indexOf to work ???



sbguy
03-21-2012, 02:21 PM
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>

devnull69
03-21-2012, 02:27 PM
new String() is obsolete. If you want to create a string, just assign a string value to a variable

Examples:


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?

webdev1958
03-21-2012, 02:28 PM
input_index = new String(input_array.indexOf(div_id));In the above line, where is div_id getting its value from?

webdev1958
03-21-2012, 02:41 PM
My suggestion would be to ditch the jquery and use just plain javascript for something relatively basic like this.


<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum