Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 822315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:46:51+00:00 2026-05-15T02:46:51+00:00

I am a beginner in jQuery. It would be great if somebody could help

  • 0

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.

<html><br />
&lt;head&gt;<br />
&lt;title&gt;<br />jQuery&lt;/title&gt;<br />
&lt;script type="text/javascript" language="javascript" src="jquery.js"&gt;<br />&lt;/script&gt;<br />
&lt;script type="text/javascript" language="javascript"&gt;<br />
 $(document).ready(function() {<br />
  var elem = $('.spanClass1').get();<br />
  alert(elem.length);<br />
  alert("Text box elements " + $('input[type=text]').get().length);<br />
  if (elem.length &gt; 0)<br />
  {<br />
   var elem1 = $(elem[0]).prev('input[type=text]');<br />
   $(this).scrollTop($(elem1).position().top);<br />
   $(elem1).css('background-color','red');<br />
   $(elem1).focus();<br />
  }<br />
  });<br />
&lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
 &lt;br /&gt;<br />
 &lt;div&gt;<br />
 &lt;input type="text" id="text1" value="test" class="selected"&gt;<br />&lt;span id="elem4" class="sClass1"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/span&gt;<br />&lt;span id="elem5" class="spanClass1"&gt;<br />This is a test&lt;/span&gt;<br />&lt;br /&gt;<br />
 &lt;/div&gt;<br />
&lt;/body&gt;<br />
</html>

Thanks in advance.

Regards,
Karthik

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:46:52+00:00Added an answer on May 15, 2026 at 2:46 am

    The call .prev() returns only the previous element, not other elements that come before it.

    You want the .prevAll() call:

    var elem1 = $(elem[0]).prevAll('input[type=text]');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.