Nokogiri id attribute not functioning

23 views
Skip to first unread message

Edsil Basadre

unread,
Jun 21, 2015, 8:06:01 AM6/21/15
to rubyonra...@googlegroups.com
I'm building an app in rails using Nokogiri .. I used puts documents.css("._Ak cite").length and I get the exact length I want but when I tried the id part of the of this puts documents.css("div#mbEnd > cite").length I didn't get the exact, makes 0 length. 

I don't know why nokogiri not recognizing the tag. 

Frederick Cheung

unread,
Jun 21, 2015, 12:12:08 PM6/21/15
to rubyonra...@googlegroups.com, eebas...@gmail.com
On Sunday, June 21, 2015 at 3:06:01 PM UTC+3, Edsil Basadre wrote:
> I'm building an app in rails using Nokogiri .. I used puts documents.css("._Ak cite").length and I get the exact length I want but when I tried the id part of the of this puts documents.css("div#mbEnd > cite").length I didn't get the exact, makes 0 length. 
>
>
> I don't know why nokogiri not recognizing the tag. 

Sounds like it just doesn't match your document but without seeing an excerpt there's not much more that can be said.

Fred

Edsil Basadre

unread,
Jun 21, 2015, 12:44:55 PM6/21/15
to rubyonra...@googlegroups.com, eebas...@gmail.com
I really don't why I can't get the data if I used id because when I tried the class it will gives the exact data. Is there any ways that can I used  id to fetch the data?

Elizabeth McGurty

unread,
Jun 21, 2015, 2:22:09 PM6/21/15
to rubyonra...@googlegroups.com
Per https://api.jquery.com/category/selectors/

Child Selector (“parent > child”)

Selects all direct child elements specified by “child” of elements specified by “parent”.

Double check how you are referencing the parent and the children

I grabbed this code from https://api.jquery.com/child-selector/, modified it and it works.  Just remember that multiple elements can have the same css class, but each element must have a

distinct css id.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>child demo</title>
  <style>
  body {
    font-size: 14px;
  }
      </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<ul class="topnav">
  <li>Item 1</li>
  <li>Item 2
    <ul>
    <li>Nested item 1</li>
    <li>Nested item 2</li>
    <li>Nested item 3</li>
    </ul>
  </li>
  <li>Item 3</li>
</ul>

<ul id="topnav_id">
  <li>Item 1</li>
  <li>Item 2
    <ul>
    <li>Nested item 1</li>
    <li>Nested item 2</li>
    <li>Nested item 3</li>
    </ul>
  </li>
  <li>Item 3</li>
</ul>
<script>
myChildren = $( "ul#topnav_id > li" );
myChildren.css( "border", "3px double blue" );
alert("With id " + myChildren.length);

myChildren = $( "ul.topnav > li" );
myChildren.css( "border", "3px double purple" );
alert("With class " + myChildren.length);

</script>
 
</body>
</html>


Hope this helps.
Liz
Reply all
Reply to author
Forward
0 new messages