views:

87

answers:

1

Hi to all,

Is it possible using jQuery to select one full link which href contains another variable?

My code is:

var element1 = $(".not-viewed").parents('li').attr("id");

var element2 = $(".videoTitle").attr("href");

Where i need to select a full link that contains 'element1',because there several videos in the page.In my code select the first on the page,but i need the specific with id.. Example..[In this example the code that i need is: 1581889025]

    <li class="videoContainer vidLink" id="1581889025">
    <a href="site.com../view/1581889025:49_3eA8F7sPr5FQGZcl2yzx3dz5Y_XwP">
    <span class="HOVER"></span>
    <div class="videoX"><img id="clThumb_1581889025" src="PREVIEW.jpg">
    </div>  </a>
    <a class="videoTitle" href="site.com../view/1581889025:49_3eA8F7sPr5FQGZcl2yzx3dz5Y_XwP">Title of Video</a>
<span class='not-viewed'></span>
<div class="tooltip-footer"></div>
</li>

Thanks in advance. Kind regards.

+2  A: 

You can use the attribute-contains selector (*=), like this:

var id = $(".not-viewed").parents('li').attr("id");
var href = $(".videoTitle[href*='" + id + "']").attr("href");

You can give it a try here, you may also want $(".videoTitle[href*='/" + id + ":']") to narrow it down to /1581889025: matches, in case there was a 15818890250 for example.

Nick Craver
var href = $("a.videoTitle[href*='" + id + "']").attr("href"); - You should add "a" in the selector as other markup may also contain videoTitle class. (Although it doesn't matter much but I think it will be a bit faster as well.)
Vikash
@Vikash - It depends on the browser we to which is faster :) But yes that's a valid selector as well, my point was more to demonstrate the attribute portion while only clarifying, not changing the rest :)
Nick Craver
Thanks to all,work all perfectly :-) I add all your names in my project ;-) Very thanks..
Luca
@Luca - Welcome :) be sure to accept answers if they resolve your question!
Nick Craver
Done,thanks again :-)
Luca