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

LVL 12
Nathan RileyFounderAsked:
Who is Participating?
 
Julian HansenCommented:
Class - if you have many and want to address many
ID - if you want to target specific element on the page.
If you use the class method and you want to find the element with a given class relative to another element example
<div class="parent">
    <input type="checkbox" class="checkme" />
    <div class="someotherclass">
           <div class="target"> ...</div>
    </div>
</div>

Open in new window

You have various options.

1. Goto the common parent and find from there

$('.checkme').click(function() {
  $(this).closest('.parent').find('.target').html('Some text');
});

Open in new window


2. If the structure of your elements is such that you know exactly where one is in relation to another then

$('.checkme').click(function() {
  $('.target', $(this).next()).html('Just to show this works');
});

Open in new window


Full sample here
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  $('.checkme').click(function() {
    $('.target', $(this).next()).html('fred');
  });
  $('.checkme2').click(function() {
    $(this).closest('.parent').find('.target').html('Some text');
  });  
});
</script>
<style type="text/css">
</style>
</head>
<body>
<div class="parent">
    <input type="checkbox" class="checkme" />
    <div class="someotherclass">
           <div class="target"> ...</div>
    </div>
</div>
<div class="parent">
  <input type="checkbox" class="checkme2" />
  <div class="someotherclass">
    <div class="target"> ...</div>
  </div>
</div>
</body>
</html>

Open in new window

0
 
Nathan RileyFounderAuthor Commented:
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

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

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Nathan RileyFounderAuthor Commented:
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

0
 
Julian HansenCommented:
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

0
 
Nathan RileyFounderAuthor Commented:
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.
0
 
Julian HansenCommented:
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.
0
 
Nathan RileyFounderAuthor Commented:
Got it, so it should be a class instead?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.