Reputation:
I am having a problem with my javascript that I can't quite explain. Here is the html:
<body>
<div id='slidecarousel' class='slide1' onclick='No functions can be here'>
<div id="sliderButton" onclick='pageSlider(this)'>Next Slide</div>
<div id='piece of slider...'></div>
<div...></div>
...
</div>
</body>
Here is the javascript and the error:
function pageSlider(elem)
{
var pDiv = $(elem).parent('#slidecarousel')
if ( pDiv.className.match(/(?:^|\s)slide1(?!\S)/) ){
**Uncaught TypeError: cannot call method 'match' of undefined**
pDiv.className = "slide2";
} else {
pDiv.className = "SlideErr";
}
}
Upvotes: 3
Views: 72
Reputation: 707846
pDiv is a jQuery object, NOT a DOM element so it doesn't have a className property. You can get the first DOM element with:
pDiv[0]
So, just change this:
var pDiv = $(elem).parent('#slidecarousel')
to this:
var pDiv = $(elem).parent('#slidecarousel')[0];
As an additional change, this doesn't make a whole lot of sense:
var pDiv = $(elem).parent('#slidecarousel')
If you just want the #slidecarousel
object, then just use:
var pDiv = $('#slidecarousel')[0];
because there can only be one #slidecarousel
object in the whole page anyway.
Or, if you just want the parent of elem
, then use this:
var pDiv = elem.parentNode;
Or, you could also use .closest()
like this:
var pDiv = $(elem).closet(".slide1")[0];
Upvotes: 1
Reputation: 782064
Use jQuery's method for testing classes:
if (pDiv.hasClass("class1"))
Upvotes: 1
Reputation: 18354
You should do:
pDiv[0].className.match(
or
pDiv.attr("class").match(
Cheers
Upvotes: 0