Mcam435
Mcam435

Reputation: 101

jQuery and CSS not working when generated from PHP

I'm trying to create a follow button (like twitter) and it's largely working, however when I generate the HTML from PHP script it's not.

Here's the fiddle to show it working: http://jsfiddle.net/MCam435/HpAWH/10/

Works wonderfully :)

However when I generate it from PHP:

PHP Script

function getTalent($row) {

     return "<div class='talent_result_wrapper' data-experience='" . $row['divTagExp'] . "' data-salary='" . $row['divTagSal'] . "'>
      <div class='talent_result_header'>
        <span class='talent_result_head'>ID: </span>" . $row['CandidateID'] . "
      </div>
        <ul>
          <li><strong>Resides:  </strong>" . $row['Town'] . "</li>
          <li><strong>Salary Required:  </strong>£" . $row['SalaryMin'] . "</li>
          <li><strong>Experience:  </strong>" . $row['CandidateExperience'] . " Years </li>
          <li><strong>Industy:  </strong>" . $row['PrimarySector'] . "</li>
          <li><strong>Specialism:  </strong>" . $row['PrimarySector'] . "</li>
          <br>
          <div id='follow1'><a href='#' class='follow' id='1'><span class='follow_b'> + Tag </span></a></div>
          <div id='remove1' style='display:none'><a href='' class='remove' id='1'><span class='remove_b'> - UnTag </span></a></div>
      </div>";
    }

Main Page

while($row = mysqli_fetch_array($result2)) {
                echo getTalent($row);
            }   

The switching between div's doesn't work, even though the output is exactly the same?

Upvotes: 0

Views: 128

Answers (1)

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26163

You can't have multiple elements with the same ID on a page.

     <div id='follow1'><a href='#' class='follow' id='1'><span class='follow_b'> + Tag </span></a></div>
     <div id='remove1' style='display:none'><a href='' class='remove' id='1'><span class='remove_b'> - UnTag </span></a></div>

You need to give those elements all unique IDs. It will work if there is only 1 instance of this on the page, but as soon as you add more than 1 it will start to fail. That's why it works in your example, but not when you use PHP.

Upvotes: 3

Related Questions