Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

jQuery Selection Help

How can I select the input value of #assetID from the below when I click on .check

<div class="col-md-12 noPadLeft raHolder" style="margin-top:20px;padding-right:30px;">
                    <input type="hidden" id="assetID" value="91">
                <div class="col-md-7" style="margin-bottom:10px;">
                    <img src="images/postedDateTwitter.png" style="margin-right:5px;">  <span class="postedDate">Scheduled for May 09 2015 at 02:00 PM</span>
                </div>
                <div class="clearfix"></div>
                <div class="col-md-12 borderB noPadLeft noPadRight" style="margin-left:15px;margin-right:15px;">
                    <div class="col-md-9 noPadLeft noPadRight" style="margin-bottom:20px;font-size:14px;color:#696a6c;">
                        Picture of logo.                    </div>
                    <div class="col-md-3  noPadLeft noPadRight text-right">
                        <div class="pull-right"><a href="#">EDIT</a></div><div class="checkDeny pull-right" style="margin-left:10px;margin-right:10px;"></div><div class="check pull-right"></div>
                    </div>
                </div>
            </div>

Open in new window


Here is my current jquery code, just not sure how to select the assetID.

<script>
    $(".check").click(function(){
        $.ajax({
            type: 'POST',
            url: 'submit.php',
            data:
            {
                assetID: $("#ueCoName").val(),
                orgID: <?php echo $orgID;?>,
                userID: <?php echo $userID;?>,
                submitType: 9

            },
            success: function(r)
            {

                $(this).closest(".raHolder").fadeOut("slow");

            }
        });
    });


</script>

Open in new window

Avatar of Nathan Riley
Nathan Riley
Flag of United States of America image

ASKER

Tried this, but not working.

<script>
    $(".check").click(function(){
        $.ajax({
            type: 'POST',
            url: 'submit.php',
            data:
            {
                assetID: $(this).closest("#assetID").val(),
                orgID: <?php echo $orgID;?>,
                userID: <?php echo $userID;?>,
                submitType: 9

            },
            success: function(r)
            {

                //Doesn't exist
                //alert('Email preferences updated');
                //console.log(r);
                $(this).closest(".raHolder").fadeOut("slow");

            }
        });
    });


</script>

Open in new window

How about this?
$(this).parent('div.col-md-3').next('input#assetID').val()

Open in new window

Not sure, just ended up getting it done with this, not sure if you way or this is better?

$(".check").click(function(){
        $.ajax({
            type: 'POST',
            url: 'submit.php',
            data:
            {
                assetID: $(this).closest(".raHolder").find('input').val(),
                orgID: <?php echo $orgID;?>,
                userID: <?php echo $userID;?>,
                submitType: 9

            },
            success: function(r)
            {

                //Doesn't exist
                //alert('Email preferences updated');
                //console.log(r);


            }
        });
        $(this).closest(".raHolder").fadeOut("slow");
    });

Open in new window

Confused

#assetId implies an id - which must be unique per page

Therefore, the correct way to do this is
$('#assetId').val();

Open in new window

The assetID is not unique, it's dynamic and inserted during page load via php.  

I have several #assetID's on the page in each of the boxes.  Maybe I named incorrectly?  But seems to be working now with the above solution.
id's by definition must be unique - if you have multiple elements on your page with the same ID then your page is not valid and you should consider changing your design.

IF you do have multiple then you are going to run in to serious problems with selectors in javascript / jquery.

My advice - change your id's names.
Got it, so it should be a class instead?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial