0
<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>

(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   ??
}

4 Answers 4

1

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/

Sign up to request clarification or add additional context in comments.

Comments

0

Here is one way to get it:

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

Comments

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

Comments

0

try this:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.