开发者

how to get the right class name innerHTML content?

<div class="Name">
    <div class="Image"></div>

    &开发者_开发技巧lt;ul>
                <li><h5><span class"myName">I want this text</span></h5></li>
                  <li class="description">The description</li>
        </ul>
        <button class="myButton">My Button</button
</div>

(multiple items with the same structure)

if my structure is constructed this way, how can I get the class myName to have its content as a variable?

$(".myButton").click(function () {
    var itemName =    $(this).siblings(".myName").innerHTML   ??
}


Ok, so the problem is that you have an error in you HTML. You have

<span class"myName">

instead of

<span class="myName">

and this makes all difference, because your class selector will not work property. Oh, you're also missing the ">" in the closing of button tag.

Here's the fixed HTML:

<div class="Name">
    <div class="Image"></div>
    <ul>
        <li><h5><span class="myName">I want this text</span></h5></li>
        <li class="description">The description</li>
    </ul>
    <button class="myButton">My Button</button>
</div>

And here's the jQuery that works with it:

$(".myButton").click(function() {
    var itemName = $(this).parent().find('.myName').html();
    alert(itemName);
});

You had missed the closing parenthesis as well...

I created a fiddler so you can test it: http://jsfiddle.net/juj3W/


Here is one way to get it:

$(".myButton").click(function () {
  var itemName = $(this).parent().find(".myName").html();
}


var itemName = $(this).prev('ul').find('.myName').html();


try this:

$(".myButton").click(function () {
    var itemName = $(".myName", $(this).parent()).text();
                   //find class in parent
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜