Forum: Rails Spinoffs (closed, excessive spam) I have got a problem with Element.previous()

B3e14f846af915266125905d6b3edaea?d=identicon&s=25 LinkChang (Guest)
on 2008-06-09 09:33
(Received via mailing list)
the following is the examples

   <span id="test">test text</span>
   <span id="btnAddAnswer" style="display:none;"></span>
   $('btnAddAnswer').previous(0); -> test

now if the first "span"  was changed to "li"

     <li id="test">test text</li>
    <span id="btnAddAnswer" style="display:none;"></span>
    $('btnAddAnswer').previous(0); -> undefined

if it was changed like this:

     <li id="test">
          <p id="testp">test text</p>
     </li>
    <span id="btnAddAnswer" style="display:none;"></span>
   $('btnAddAnswer').previous(0); -> testp

I want only to get the value:li#test.
What should i do?
And Why was there such a phenomenon

thanks!
F490e41cd5de1393a9f6958b70dae6ab?d=identicon&s=25 Frederick Polgardy (Guest)
on 2008-06-09 15:37
(Received via mailing list)
What browser are you using?  All your examples work correctly for me in
Firefox.

It's also worth noting that your failing examples are all incorrect
HTML.
There's never a valid case where a SPAN can be a sibling of an LI
because LI
can only come directly below UL and OL, and it's the only thing that can
come below it.

-Fred

On Mon, Jun 9, 2008 at 2:32 AM, LinkChang <herts@qq.com> wrote:

>
> the following is the examples
>
>   <span id="test">test text</span>
>   <span id="btnAddAnswer" style="display:none;"></span>
>   $('btnAddAnswer').previous(0); -> test


--
Science answers questions; philosophy questions answers.
F2c683e7dc0c4d4bcc790f87eaa67301?d=identicon&s=25 RobG (Guest)
on 2008-06-10 06:06
(Received via mailing list)
On Jun 9, 5:32 pm, LinkChang <he...@qq.com> wrote:
> the following is the examples
>
>    <span id="test">test text</span>
>    <span id="btnAddAnswer" style="display:none;"></span>
>    $('btnAddAnswer').previous(0); -> test

The previous method works on siblings (ignoring #text nodes), here
"test" is the previous sibling of "btnAddAnswer".


> now if the first "span"  was changed to "li"
>
>      <li id="test">test text</li>
>     <span id="btnAddAnswer" style="display:none;"></span>
>     $('btnAddAnswer').previous(0); -> undefined

That is not valid HTML - a span can't be a sibling of an li element.
Browsers will do something with it (maybe put the span inside the li,
maybe wrap it in it's own li, maybe put it outside the ol or ul), but
whatever one browser does you can't depend on all browsers doing the
same thing.


> if it was changed like this:
>
>      <li id="test">
>           <p id="testp">test text</p>
>      </li>
>     <span id="btnAddAnswer" style="display:none;"></span>
>    $('btnAddAnswer').previous(0); -> testp

More invalid HTML, see above.  As written, the span is a sibling of
the li.  Your browser seems to have employed error correction to put
the span inside the same li as the p element, so the result (in your
browser) is:

     <li id="test">
          <p id="testp">test text</p>
          <span id="btnAddAnswer" style="display:none;"></span>
     </li>


> I want only to get the value:li#test.

Then use $('test') to reference the element.  What do you think its
value is or should be?


> What should i do?

Fix the HTML.


> And Why was there such a phenomenon

Because you are writing invalid HTML.  Use a validator:

<URL: http://validator.w3.org/ >


--
Rob
This topic is locked and can not be replied to.