Find nested children of li - javascript

I know it is ugly, but I need to find out how to get the attribute of <a> tag, of each <li> that has a <span> with class jqx-checkbox-check-checked, if the <li> contains another set of <ul><li>. Only direct <span> will be check for the desired class(jqx-checkbox-check-checked)
This is the code sample:
<li>
<span></span>
<div>
<div>
<div class="jqx-checkbox-default jqx-rc-all">
<span class="jqx-checkbox-check-checked"></span>
</div>
</div>
</div>
<span class="jqx-rc-all jqxtree item">
<b style="color:black"> item1</b>
</span>
<ul class="jqx-tree-dropdown" >
<li >
<span class="jqx-tree-item-arrow-collapse jqx-disableselect"></span>
<div class="chkbox jqx-checkbox">
<div>
<div class="jqx-checkbox-default jqx-rc-all">
<span class="jqx-checkbox-check-checked"></span>
</div>
</div>
</div>
<span class="jqx-rc-all jqx-tree-item">
item2
</span>
</li>
<li >
<span class="jqx-tree-item-arrow-collapse jqx-disableselect"></span>
<div class=""><div>
<div class="jqx-checkbox-default jqx-rc-all" >
<span class="jqx-checkbox-check-checked"></span>
</div></div></div>
<span class="jqx-rc-all jqx-tree-item">
<b style="color:black"> item3</b>
</span>
</li>
</ul>
</li>
</ul>
</li>
I know it is not simple! but if someone can show me the right jquery approach, I will be more then grateful

I dont fully understand your question - but I think you need to have a read of the following jQuery methods :
closest()
next()
find()
Using a combination of those you might be able to get a handle on the object you need
Done it :
$('.jqx-checkbox-check-checked').each(function(){
alert($(this).parent().parent().parent().next().find('a')
.attr('href'));
});
Working example : http://jsfiddle.net/manseuk/CEYXc/
Your HTML is all over the place ...

Related

Recovering multi id at a time

I try to put in place a system popup, why I decided to use the data attribute.
I wish that when an element has the id "popup" I recovered using the jQuery data-item value.
The data-item value has the id to display in my popup, my problem is that if I have several id popup, recovered came only 1 single id, the first in this page.
How can I do to retrieve all the popup and display ID is the corresponding value with the data-item?
<div id="boxpop">
<div class="centered">
<span></span>
<div class="close-btn"></div>
</div>
</div>
<div id="login" style="display:none;">
Test div
</div>
<header>
<div id="MainHeader">
<div class="logo"></div>
<nav id="Menu">
<li>
<span class="icon"></span>
<span class="text">Click me!</span>
</li>
<li>
<span class="icon icone card"></span>
<span class="text">Shop info</span>
</li>
</nav>
</div>
</header>
<script type="text/javascript">
$("#popup").click(function() {
a = $("#popup").data("item");
alert(a);
});
function demo(div) {
$("#boxpop").fadeIn(500);
$("#boxpop .centered span").empty();
$(div).insertAfter("#boxpop .centered span").fadeIn(100);
}
</script>
ID must be unique.
You should use class for this, this is the correct way:
<li>
<span class="icon"></span>
<span class="text">Click me!</span>
</li>
<li>
<span class="icon icone card"></span>
<span class="text">Shop info</span>
</li>
And then...
$(".popup").click(function() {
a = $(this).data("item");
alert(a);
});
Hope it helps.
The is is because cannot have multiple elements with the same id attribute in the same document - it's invalid HTML. You should use classes instead. From there you can use the this keyword in the click handler to reference the element which raised the event and read the data attribute. Try this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="boxpop">
<div class="centered">
<span></span>
<div class="close-btn"></div>
</div>
</div>
<div id="login" style="display:none;">
Test div
</div>
<header>
<div id="MainHeader">
<div class="logo"></div>
<nav id="Menu">
<li>
<span class="icon"></span>
<span class="text">Click me!</span>
</li>
<li>
<span class="icon icone card"></span>
<span class="text">Shop info</span>
</li>
</nav>
</div>
</header>
<script type="text/javascript">
$(".popup").click(function() {
a = $(this).data("item");
alert(a);
});
function demo(div) {
$("#boxpop").fadeIn(500);
$("#boxpop .centered span").empty();
$(div).insertAfter("#boxpop .centered span").fadeIn(100);
}
</script>

Find then Replace Class and text to Button

Given script find text from .label (text1 or any text), then it add class class="text1" to parent list <li> like this: <li class="text1">
Jquery:
$('.label a').each(function() {
$(this).closest('li').addClass($(this).text());
});
HTML:
<div id="main-id">
<button class="one"></button>
<button class="two"></button>
<ul>
<li> <!--List 1-->
<span class="meta">
<div class="label">
text1 <!--This text-->
</div>
</span>
</li>
<li> <!--List 2-->
<span class="meta">
<div class="label">
text2 <!--This text-->
</div>
</span>
</li>
</ul>
</div>
By this Script we get Result:
<ul>
<li class="text1"> <!--Got New class to List 1-->
<span class="meta">
<div class="label">
text1
</div>
</span>
</li>
<li class="text2"> <!--Got New class List 2-->
<span class="meta">
<div class="label">
text2
</div>
</span>
</li>
</ul>
Please See Example Fiddle >
Now following my question i'm trying to replace Class and Text to Buttons, there have two buttons like <button class="one"></button> and <button class="two"></button>, now want to place class/text .text1 (or any class) inside button,
like: <button class="one text1">text1</li></button> and <button class="two text2">text2</li></button> by same jquery given above.
I have tried this Replace text script, but i'm unable to do that. Any suggestion for other way to do this?
Thanks in advance.
Not sure exactly what you are going for but you could try something like this.
<html>
<body>
<div id="main-id">
<button class="one"></button>
<button class="two"></button>
<ul id="main-list">
<li>
<span class="meta">
<div class="label">
text1
</div>
</span>
</li>
<li>
<span class="meta">
<div class="label">
text2
</div>
</span>
</li>
</ul>
</div>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(function(){
var btns = ['.one', '.two'];
$('#main-list li').each(function(k,el){
// use clone if you don't want to remove original dom element
var $li = $(el).clone();
var className = $li.find('a').text();
$li.addClass(className)
.empty()
.text(className);
var $btn = $(btns[k]);
$btn.append($li);
});
});
</script>
</body>
</html>

How to click on the element using jQuery which generates dynamic ID on the click of the element?

<div class="scrolling-items-wrapper">
<div class="menu" style="max-height: 105px;">
<div class="scrolling" style="width: 196px; max-height: 105px;">
<div class="scrolling-content">
<ul >
<li data-menuid="ABC_1">
<a style="white-space:nowrap;" href="#">
<span class="checkbox-container" href="#">
<span class="checkbox" visibility="hidden"></span>
</span>
<span>ABC_1</span>
</a>
</li>
<li data-menuid="ABC_2">
<a style="white-space:nowrap;" href="#">
<span class="checkbox-container" href="#">
<span class="checkbox" visibility="hidden"></span>
</span>
<span>ABC_2</span>
</a>
</li>
<li data-menuid="ABC_3">
<a style="white-space:nowrap;" href="#">
<span class="checkbox-container" href="#">
<span class="checkbox" visibility="hidden"></span>
</span>
<span>ABC_3</span>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
Manual click :
Whenever that element is clicked manually it generates a dynamic ID.
Using jQuery :
I want to click a particular element suppose ABC_1 using jQuery.
When it is clicked it should generate the Dynamic ID.
Did you try this?
$( "li[data-menuid='ABC_1']" ).click()
If it doesn't work here is a list of all jquery selectors: https://api.jquery.com/category/selectors/
If you have al element with ID ABC_123, you can do this:
$(document).ready(function() {
$("#ABC_123").click(function () {
var newId = "ABC_"+new Date().getTime();
this.id= newId;
});
});
This will change the id of the element by adding the date in miliseconds. Then, do what you want with the new ID.

CSS3 How do i add a hyperlink to <div data-image="#">

sorry i've tried asking this before and got no good results, so here's trying again.
I have tried many things and still haven't had any good results.
The code is here -> PasteBin
I want to add a hyperlink to the data-images for the ninja slider. I've never encountered data-image before but i can't use img src="#" cause that doesn't work with the javascript if you guys need any more information, let me know. This is driving me insane!
<div id='ninja-slider'>
<ul>
<li>
<div data-image="images/md/1.jpg"> </div>
</li>
<li>
<div data-image="images/md/2.jpg"> </div>
</li>
<li>
<div data-image="images/md/3.jpg"></div>
</li>
<li>
<div data-image="images/md/4.jpg"></div>
</li>
</ul>
</div>
this is the troublesome part.
Simply enclose your <div>s within an <a> tag. That way the <div>s are technically links.
<div id='ninja-slider'>
<ul>
<li>
<a href="images/md/1.jpg">
<div data-image="images/md/1.jpg"></div>
</a>
</li>
<li>
<a href="images/md/2.jpg">
<div data-image="images/md/2.jpg"></div>
</a>
</li>
<li>
<a href="images/md/3.jpg">
<div data-image="images/md/3.jpg"></div>
</a>
</li>
<li>
<a href="images/md/4.jpg">
<div data-image="images/md/4.jpg"></div>
</a>
</li>
</ul>
</div>
you can use java script to add link
Example:
<div onclick="javascript:location.href='http://www.google.com/'" data-image="images/md/1.jpg"> </div>
Also you can use css to change the pointer
#ninja-slider ul li div{
cursor:pointer;
}
Thanks..
Try using:
<div onclick="location.href='http://www.example.com';" />
Or use span or a tags instead
<style type="text/css">
.myspan {
display: block;
}
</style>
<span class="myspan">text</span>

adding an div in autosuggest

i am implementing the bsn autosuggest
can any one tellme how can i add an div in the result the result will be formed like this
<div style="left: 347px; top: 1024px; width: 400px;" class="autosuggest" id="as_testinput_xml">
<div class="as_header">
<div class="as_corner"></div>
<div class="as_bar"></div>
</div>
<ul id="as_ul">
<li>
<a name="1" href="#">
<span class="tl"> </span>
<span class="tr"> </span>
<span><em>W</em>aldron, Ashley<br><small>Leicestershire</small></span>
</a>
</li>
<li>
<a name="2" href="#">
<span class="tl"> </span>
<span class="tr"> </span>
<span><em>W</em>heeler, Bysshe<br><small>Lincolnshire</small></span>
</a>
</li>
</ul>
<div class="as_footer">
<div class="as_corner"></div>
<div class="as_bar"></div>
</div>
</div>
i need to add an diva above the ul as the Category like
<div>Category</div>
i tried
var tmp = _bsn.DOM.createElement("div", {className:"tmp"});
and i need to know how to add values to the div using java script .....
This autocomplete system hasn't been updated since 2007. I think it'd be wise for you to try something that's more up to date. Do you use jQuery? This is what I'd recommend: http://docs.jquery.com/Plugins/autocomplete, it handles all the work for you.
An advantage with using jQuery is that you can utilize Google's CDN: http://css-tricks.com/google-cdn-naming/
There's a very high chance your visitor won't have to download jQuery cause it's already been used on another site.

Categories