views:

115

answers:

1

I use too much jquery

I want to do this with out jquery:

<a onclick="javascript:$(this).next().css('display', 'none')">

I thought it was this:

<a onclick="javascript:this.nextSibling.style.display = 'none'">

But its not. Style returns undefined.

Let me clarify:

I want to do this:

<a class="errorToggle">error</a>
<div style="padding-left:20px;margin:0 0 10px 0;display:none;">

<h4>A PHP Error was encountered</h4>

<p>Severity: <?php echo $severity; ?></p>
<p>Message:  <?php echo $message; ?></p>
<p>Filename: <?php echo $filepath; ?></p>
<p>Line Number: <?php echo $line; ?></p>

</div>

<script type="text/javascript">
  $(".errorToggle").click(function(){
    $(this).next().css("display", "block");
  });
</script>

Without jQuery BECAUSE this is a code igniter error view file that quite often will load before my header does, so I will not have access to jquery. It doesn't need to be pretty, or well coded, just work. This will not be used in production.

+5  A: 

Take a look at this article:
JavaScript nextSibling and Cross Browser Compatibility

Excerpt:

The problem was that we were referencing the 'nextSibling' of an element in our JS using the onClick event. In IE this worked great, however in Firefox the 'nextSibling' could be a line break.

You might want to iterate through the "nextSibling()" until you hit what you want.

o.k.w
This is WHY you use a library like jQuery -- cross browser assured same-behavior. Write once, just works.
Erik
This is also WHY many of us are crippled without these libraries and frameworks. I'm one of those as well. Hah
o.k.w
Note also that it's Firefox's behaviour that is correct.
bobince
@bobince: Really? Any reference?
o.k.w
It's one of those areas that fell between the cracks in that SGML says nothing at all about DOMs (only rendering) and W3 DOM said nothing at all about parsing, until DOM Level 3 Core and LS (which IE does not claim to support). Under DOM 3 LS one is to include all text content by default, unless the element-content-whitespace parameter is set to false, in which case whitespace-only text nodes are omitted in elements that the doctype defines as containing only other elements. (Most HTML elements have mixed content and not element content anyway.)
bobince
This is finally cleared up for good by HTML5 section 3.2.5 (though of course HTML5 is not yet a complete standard).
bobince
@bobince: Thanks for the info. Very enligthening indeed.
o.k.w