Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6176869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:10:38+00:00 2026-05-24T00:10:38+00:00

This is my HTML structure. <a href=www.example.com rel=group-1 class=project> <img src=http://www.example.com/image.png> </a> <div class=data>

  • 0

This is my HTML structure.

<a href="www.example.com" rel="group-1" class="project">
    <img src="http://www.example.com/image.png">
</a>
<div class="data">
    <span class="media">http://www.example.com/video.mp4</span>
    <div class="desc">
        <p>asdfs</p>
        <a href="http://www.example.com/link">view link</a>
    </div>
</div>

I want to use jQuery to attach a class onto the tag based upon the HTML from the media class (span.media) in its sibling div (div.data)

For example:
The above HTML would attach “video” to the tag based upon the “.mp4”
If it was http://www.example.com/image.jpg or http://www.example.com/image.png then it would attach “image” to the class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:10:39+00:00Added an answer on May 24, 2026 at 12:10 am

    How about:

    $(".media").addClass(function() {
        var text = $(this).text();
        if (~text.indexOf("mp4")) {
            return "video";
        } else if (~text.indexOf("jpg") || ~text.indexOf("png")) {
            return "image";
        }
    });
    

    using the version of .addClass that takes a function (I’m not entirely clear which tag you want to add the class to, but this should get you started in the right direction).

    Also uses the bitwise not operator ~ to turn the -1 result of indexOf into 0 (a falsey value)

    Example: http://jsfiddle.net/andrewwhitaker/NaKW4/


    If you want to add the class to div.data:

    $(".data").addClass(function() {
        var text = $(this).find(".media").text();
        if (~text.indexOf("mp4")) {
            return "video";
        } else if (~text.indexOf("jpg") || ~text.indexOf("png")) {
            return "image";
        }
    });
    

    Example: http://jsfiddle.net/andrewwhitaker/vKPy2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this html: <div class=nation> <a href=library.php?type=nation&amp;id=America> <div class=nation-image> <img src=nations/America.png alt=snip /> </div>
I have this HTML structure <img width=25 height=25 src=/data/apple_logo.jpg alt=Chocolate /> <input class=cake_checkboxes style=vertical-align:
http://something.com:80/somedir/index.html?type=test;one=onevalue#nose This is typical URI structure my question is what is #nose and how
I have a simple HTML structure ( jsfiddle ): <li> <div class=buttons> <a href=done><img
For example, I have this HTML structure: <ul> <li><a class=ac_list_a>some text</a></li> <li><a class=ac_list_a>some text</a></li>
I have this HTML structure: <ul class="shopp_categories"> <li><a href="#">TEXT</a> <ul class="children"> <li><a href="#">TEXT</a></li> </ul>
I have this html structure: <ul class=shopp_categories> <li><a href=#>TEXT</a> <ul class=children> <li><a href=#>TEXT</a></li> </ul>
Two jQuery questions: 1) Given this HTML structure <div id=tipper class=tiplink><a href='test.html' >Link Text</a>
I have this HTML structure (excerpt): <td> <select id=test1 class=pub_chooser> <option value=99>All Publications</option> <option
I have this html structure: <div id=home> <div class=section_header> <h1 class=left data-target=newest><?=lang('home_newest');?></h1> ... </div>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.