Give a div class an ID based on previous class

Hi there,

I have the following markup:

<div class="wtrShtTrainerStream wtrTwoCols  clearfix">
  <div class="wtrSht wtrShtTrainer ">
    <div class="wtrShtTrainerMeta ">
      <div class="wtrShtTrainerMetaName wtrShtTrainerAnimate">
        <h5 class="wtrShtTrainerMetaNameHeadline wtrShtTrainerAnimate">NAME 1</h5>
      </div>
    </div>
  </div>
  <div class="wtrSht wtrShtTrainer wtrShtTrainerLastInRow">
    <div class="wtrShtTrainerMeta ">
      <div class="wtrShtTrainerMetaName wtrShtTrainerAnimate">
        <h5 class="wtrShtTrainerMetaNameHeadline wtrShtTrainerAnimate">NAME 2</h5>
      </div>
    </div>
  </div>
</div>

Is there a way I can give the <div> with the class of “wtrShtTrainer” an ID based on the parent class “wtrShtTrainerStream” so I can then target them with other jQuery?

Thanks

Why does your code need seven div elements
to display just two words of content?

You do not have a Javascript problem, what you
do have is a semantic HTML problem.

coothead

I also doubt that you’re down to h5 for the headings. :astonished:

Why can’t you use a class selector with jQuery?

var trainerDivs = $(".wtrShtTrainer");

The code is being output by a Wordpress plugin that I do t have access to. I’m trying to give the div with the class “wtrShtTrainer” an ID

var trainerDivs = $(".wtrShtTrainer");
var trainderDiv = trainerDivs.get(0);
trainderDiv.id = "wtrShtTrainerID";

You can now use jQuery(“#wtrShtTrainerID”) to reference that id.

Thanks Paul, it worked :slight_smile:

Wordpress and jQuery: 2     -    coothead and sematic HTML: 0. :unhappy:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.