Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Select button(s) inside div

Tags:

jquery

I would like to select a specific group of buttons that exist inside a DIV and assign their ID to a hidden field on the page but I cant for the life of me select the buttons inside the div.. Example fails below

SOURCE HTML

    <div id = test>
    <div class="ButtonGroupWrapper">                    
                    <img src="test.jpg" width="100" height="100" alt="thumbnail" />
                <input id="buttonID5" type="submit" value="Link" class="button" />
    </div>
    <div class="ButtonGroupWrapper">                    
                    <img src="test.jpg" width="100" height="100" alt="thumbnail" />
                <input id="buttonID6" type="submit" value="Link" class="button" />
    </div>
    </div>

Jquery selector fails

    $(".ButtonGroupWrapper").find(":button").click(function () {
        alert("hi there");
        return false;
    });

    $("#ButtonGroupWrapper input[type=button]").click(function () {
        alert("hi there");
        return false;
    });

    $("#ButtonGroupWrapper :button").click(function () {
        alert("hi there");
       return false;
    });
like image 440
RenRock Avatar asked Aug 08 '12 19:08

RenRock


People also ask

How do I get the button inside a div?

content = document. getElementsByClassName("keys")[0]; kbButtons = content. getElementsByTagName("button"); You can access the first element of the HTMLCollection with the [0] bracket syntax.

Which is the correct jQuery selector statement to select all div elements?

13. Which is the correct jQuery selector statement to select all <div> elements? Explanation: The statement $("div") is the correct syntax to select all <div> elements.

Which is the correct jQuery selector to select all button elements and input elements with type input?

The :button selector selects button elements, and input elements with type=button.


3 Answers

Putting a space looks for any .button class within your .ButtonGroupWrapper class. (NOTE: not any 'button', but anything with the code class="button" added to it)

$(".ButtonGroupWrapper .button").click(function () {
    alert("hello");
    return false;
});

In your HTML code the submit input has a 'class' of 'button'. It could actually be anything such as .strawberry or .mybutton or anything you like.

Alternative:

As you have an id wrapping the whole lot you could also target button within the id:

<div id ="test">
  <div>                    
    <img src="test.jpg" width="100" height="100" alt="thumbnail" />
    <input type="submit" value="Link" class="button" />
  </div>
  <div>                     
    <img src="test.jpg" width="100" height="100" alt="thumbnail" />
    <div class="button" />Just text</div>
  </div>
</div> 

And then your javascript could be:

$("#test .button").click(function () {
     alert("button class clicked");
    return false;
});

Remember you need to run this after the DOM has loaded, so best practice is to either write it at the end of the page or wrap it in your favourite onready function.

like image 141
Digital Visual Avatar answered Oct 04 '22 04:10

Digital Visual


This will work:

$(".ButtonGroupWrapper").find(".button").click(function () {
    alert("hi there");
    return false;
});
like image 33
wanovak Avatar answered Oct 04 '22 04:10

wanovak


Try:

$(".ButtonGroupWrapper").find(".button").click(function () {
        alert("hi there");
        return false;
});

Your first example fails because you're trying to target a class which is preceded by . not :. You may also be trying to target an element of type button but the element in question is of type submit.

Your second example fails because you're trying to select an input of type button when none exist (your target is type submit). An alternative would be input[type=submit].

Your third example fails for a similar reason to the first example failing in that it's looking for an element of type button.

See also http://api.jquery.com/button-selector/

like image 45
j08691 Avatar answered Oct 04 '22 03:10

j08691