surfbird0713
surfbird0713

Reputation: 1219

Assign unique ID to div as created and then reference with jquery

I am brand new to php and a beginner with jQuery. I have a php page that is populated with data from a mySQL table. What I am trying to achieve is for the h3 that contains "View Job" to have a unique id assigned to it, as well as the div that prints out the job description. Then, I would like to reference these with jQuery so that if someone clicks View Job, only the description for that job will show. I hope this makes sense.

I tried this with classes and of course all the job descriptions revealed themselves when any View Job was clicked.

I tried the solution here, but this ended up with 36 "View Job" links on my page, and I need to assign the unique ID to the h3 and div as they are created.

I am open to suggestions for another way to achieve what I'm looking for - basically to hide/collapse each description as the user clicks on View Job for each job.

here is my code:

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <style type="text/css">
        .job-post{border-bottom: 1px solid red; padding: 0; margin: 10px 0;}
        h3, p{padding: 0; margin:0;}
        .view-job{cursor: pointer;}
        </style>
        <script type="text/javascript">
                        $(document).ready(function() {
                            $("div#job-details").css("display" , "none");
                             $("h3#view-job").click(function() {
                                $("div#job-details").css("display" , "block");
                            });

                        });
         </script> 
        <?php 
         // Connects to your Database 
         mysql_connect("xx", "xx", "xx") or die(mysql_error()); 
         mysql_select_db("lcwebsignups") or die(mysql_error()); 
         $data = mysql_query("SELECT * FROM openjobs") 
         or die(mysql_error()); 
          ?>
         <div id="job-container"> 
          <?php 
         Print ""; 
         while($info = mysql_fetch_array( $data )) 
         { 
         Print "<div class='job-post'>"; 
         Print "<h3>Position / Location:</h3> <p>".$info['jobtitle'] . ", ".$info['joblocation'] . "</p>";
         Print "<h3 id='view-job'>View Job:</h3> <div id='job-details'>".$info['jobdesc'] . " </div>"; 
         Print "</div>";
         }  
         ?>
        </div><!--//END job-container-->

Upvotes: 2

Views: 1087

Answers (3)

cssyphus
cssyphus

Reputation: 40038

This should do what you want (tested okay). Just replace your mysql login info and the example should work.

I completed your job-details div (it was missing the data from php), but what you were really looking for is the jQuery code.

When an <h3> tag whose id begins with the characters view-job is clicked, the jQuery click event handler will:

  • re-hide all divs whose id starts with the chars job-details

  • display the next div in the DOM tree


CODE:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<style type="text/css">
    .job-post{border-bottom: 1px solid red; padding: 0; margin: 10px 0;}
    h3, p{padding: 0; margin:0;}
    .view-job{cursor: pointer;}
</style>
<script type="text/javascript">
    $(document).ready(function() {
        $("div[id^='job-details']").css("display" , "none");

        $('h3[id^="view-job"]').click(function() {
            $("div[id^='job-details']").hide(500);
            $(this).next('div').show(500);
        });

    });
 </script> 

 <?php 
    // Connects to your Database 
    mysql_connect("xx", "xx", "xx") or die(mysql_error()); 
    mysql_select_db("lcwebsignups") or die(mysql_error()); 
    $data = mysql_query("SELECT * FROM openjobs") or die(mysql_error()); 

        $data = mysql_query("SELECT * FROM openjobs") or die(mysql_error());
  ?>

<div id="job-container"> 

<?php 
    echo "<br>"; 
    while($info = mysql_fetch_assoc( $data )) { 
        echo "<div class='job-post'>"; 
        echo "<h3>Position / Location:</h3> <p>".$info['jobtitle'] . ", ".$info['joblocation'] . "</p>";
        echo "<h3>View Job:</h3> <div id='job-details'>".$info['jobdetails']."</div>"; 
        echo "</div>";
    }
 ?>

 </div><!--//END job-container-->

Upvotes: 0

Jay Patel
Jay Patel

Reputation: 5793

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<style type="text/css">
.job-details{display:none;} 
.job-post{border-bottom: 1px solid red; padding: 0; margin: 10px 0;}
h3, p{padding: 0; margin:0;}
.view-job{cursor: pointer;}
</style>
<script type="text/javascript">
$(document).ready(function() {
     // Please hide this class on CSS
     // $("div.job-details").css("display" , "none");
     $("h3.view-job").click(function() {
        // you ned to use this keyword
        // it is very important when you use javascript or jquery
        // this keyword only pick element on which you click
        $(this).parent().find(".job-details").show();
    });

});
   </script> 
    <?php 
     // Connects to your Database 
     mysql_connect("xx", "xx", "xx") or die(mysql_error()); 
     mysql_select_db("lcwebsignups") or die(mysql_error()); 
     $data = mysql_query("SELECT * FROM openjobs") 
     or die(mysql_error()); 
     ?>
     <div id="job-container"> 
     <?php 
     // First of all Please use echo instead of Print
     echo ""; 
     while($info = mysql_fetch_array( $data )) 
     { 
         echo "<div class='job-post'>"; 
         echo "<h3>Position / Location:</h3> <p>".$info['jobtitle'] . ", ".$info['joblocation'] . "</p>";
         // you can't use id multiple time in a same page so instead of id use Class
         // if you want to use id then you have to generate uniq id
         // check below I generate id --> I don't know your database field that's why I used jobId
         echo "<h3 class='view-job' id='job".$info['jobId']."'>View Job:</h3> <div class='job-details'>".$info['jobdesc'] . " </div>"; 
         echo "</div>";
     }  
     ?>
    </div><!--//END job-container-->

Upvotes: 0

Sushanth --
Sushanth --

Reputation: 55740

Assign the classNames instead of id's

Then use the this context to select your div which will only search for the one in the context and not all the divs

$("h3.view-job").on('click',function() {
    $(this).next("div.job-details").css("display" , "block");
});

Upvotes: 2

Related Questions