最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery .hide() does not work with my array - Stack Overflow

matteradmin6PV0评论

Hello and thanks for your help.

I have a php script on my website that returns my last few tweets in form of an unordered list with the posts each being a list item.

<div id="latesttweet">
  <ul>
    <li>
      This is a tweet<br />
      <span>3 hours ago.</span>
    </li>
    <li>
      This is another tweet<br />
      <span>4 hours ago.</span>
    </li>
  </ul>
</div>

I tried to hide some of them dynamically using JavaScript, but for some reason once I collect all the list items in an Array, I can't address them properly anymore. When I alert the length of the Array however, I get the right number. jQuery seems to be working alright in general.

var activeTweet = 0;
var tweet_ul = document.getElementById('latesttweet');
var tweetArray = tweet_ul.getElementsByTagName('li');

var tweetCount = tweetArray.length;
alert(tweetCount); //returns the right result

tweetArray[1].hide();

This way, one should expect that the second list item would have display set to none, but still both items remain visible.

What am I doing wrong?

Hello and thanks for your help.

I have a php script on my website that returns my last few tweets in form of an unordered list with the posts each being a list item.

<div id="latesttweet">
  <ul>
    <li>
      This is a tweet<br />
      <span>3 hours ago.</span>
    </li>
    <li>
      This is another tweet<br />
      <span>4 hours ago.</span>
    </li>
  </ul>
</div>

I tried to hide some of them dynamically using JavaScript, but for some reason once I collect all the list items in an Array, I can't address them properly anymore. When I alert the length of the Array however, I get the right number. jQuery seems to be working alright in general.

var activeTweet = 0;
var tweet_ul = document.getElementById('latesttweet');
var tweetArray = tweet_ul.getElementsByTagName('li');

var tweetCount = tweetArray.length;
alert(tweetCount); //returns the right result

tweetArray[1].hide();

This way, one should expect that the second list item would have display set to none, but still both items remain visible.

What am I doing wrong?

Share Improve this question asked Aug 22, 2012 at 11:43 maxmax 1151 silver badge9 bronze badges 4
  • $(tweetArray[1]).hide(); – Mient-jan Stelling Commented Aug 22, 2012 at 11:45
  • 1 remember array starts from 0 so try $(tweetArray[0]).hide(); – Peter Commented Aug 22, 2012 at 11:46
  • Why do you use jquery for hiding an element and normal javascript for access to the element? – Pigueiras Commented Aug 22, 2012 at 11:48
  • @PeterSzymkowski - Note that the question specified hiding the second list item. – Scott Sauyet Commented Aug 22, 2012 at 11:57
Add a ment  | 

8 Answers 8

Reset to default 2

Basically you are not using jQuery

$(tweetArray[1]).hide()

Try this:

$(tweetArray[1]).hide();

Your array are DOM objects, not jQuery objects. Try the following:

var activeTweet = 0;
var tweet_ul = $('#latesttweet');
var tweetArray = $('li');

var tweetCount = tweetArray.length;
alert(tweetCount); //returns the right result

tweetArray[1].hide();
tweetArray[1].style.display="none";
$(tweetArray[1]).hide();

This should do the job. tweetArray[1] is just an html element and it does not have hide() method defined for it.

hide() is part of jQuery. You have to use the $() method: $(tweetArray[1]).hide().

You will have to wrap tweetArray[1] into a jquery object

Try

$(tweetArray[1]).hide();

var tweets = $( '#latesttweet ul' ).children( 'li' ); 
$( tweets[1] ).hide();

http://jsbin./usiyex/2/

Post a comment

comment list (0)

  1. No comments so far