Hello,
I am a beginner in jQuery. It would be great if somebody could help me with this. I am not sure what's going wrong here.
This is what I am trying. I have a element with class as "spanClass1". Before this element there is another element and a text box element. My requirement is to scroll to that text box element and gain focus to the element. And also I added some css to see the change happening. But the problem is that the code above doesn't seem to be working. If I replace the selector in .prev(..) to 'span', it works. Am I missing something? It would be really helpful if somebody could help me with this.
<head>
<title>
jQuery</title>
<script type="text/javascript" language="javascript" src="jquery.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
var elem = $('.spanClass1').get();
alert(elem.length);
alert("Text box elements " + $('input[type=text]').get().length);
if (elem.length > 0)
{
var elem1 = $(elem[0]).prev('input[type=text]');
$(this).scrollTop($(elem1).position().top);
$(elem1).css('background-color','red');
$(elem1).focus();
}
});
</script>
</head>
<body>
<br />
<div>
<input type="text" id="text1" value="test" class="selected">
<span id="elem4" class="sClass1">
</span>
<span id="elem5" class="spanClass1">
This is a test</span>
<br />
</div>
</body>
Thanks in advance.
Regards, Karthik