Filter Last child when Hover Element - javascript

I have problem When I want to create inpector plugin of jquery, If I am select child of element should be not select parent element.
That is inpect with double border actualy only home element is inspected but that is follow by parent.
this is my jsfiddle
http://jsfiddle.net/Rp7hr/46/
and this code how my plugin called
$(function(){
//generate dropdown list
var test = $('.summary').ksinspect();
});
and I don't understand why element on iframe tag is cannot be hover ?

The effect you see is becuase of Event Bubbling.
to stop that you should use event.stopPropagation()
Here is updated DEMO
See updated
_ks.onMouseOver = function() {
var el = ktm.query(document).find('*');
el.on('mouseenter',function(e){
e.stopPropagation(); // THIS is newly added line...
var d = new Date();
var id = d.getTime() + randomID(1);
var parents = ktm.query(this).parents("*");
var getParents = _get_parents(ktm.query(this));
ktm.query(this).attr('data-key',id); //add attr data-key
ktm.query(getParents).removeClass('glare'); //remove class
ktm.query("[data-key=" + id + "]").addClass('glare'); //add class
console.log("selector = " + getParents);
console.log("cusrrent selector = " + this);
});
}

Related

jQuery $.load Not Executing

I am currently using jQuery on my Django site to reload a div once a user clicks a button.
$(document).ready(function(){
var post_list = Array.from(document.getElementsByClassName("post_container"))
for(var post in post_list){
post_list[post].id = 'post' + post;
}
var $arrows = $(".arrow");
$arrows.each(function(index){
var data = $(this).data();
var element = $(this);
element.on('click', function(event){
if(user_auth){
var currentParentElement = element.parent().parent().parent().get(0);
var id = $(currentParentElement).attr('id');
$(id).load(document.URL + ' ' + id);
}
})
})
});
From the console I can see that currentParentElement and id are pointing to the correct div to reload, but $(id).load() does not seem to be doing anything.
In the image linked below, the clicking the arrow buttons should make the green or red number change. The number does not change when the arrow is clicked, but it does change when I reload the entire page.
https://i.stack.imgur.com/T26wn.png
Your ID selector is missing the # symbol. For example, suppose the id of this target element is "myID":
var id = $(currentParentElement).attr('id');
Then the jQuery selector you're using is:
$('myID')
Which is looking for a <myID> element. There isn't one, so no matches are found, so there's nothing to call .load() on.
You could add the symbol to your selector:
$('#' + id).load(document.URL + ' #' + id);
(Note: The same correction was also made in the selector passed to load() for the same reason.)

On search/highlight click -> existing div becomes wrapped with existing span

I have a problem with javascript search and highlight text.
For example, there is existing span element and existing div element.
Problem is that if I click on search button for some reason div element becomes a child of span element.
To explain it better I have created JS fiddle to show the problem:
function highlightSearch() {
$('span').removeClass('highlighted');
var text = document.getElementById('query').value;
var query = new RegExp("(\\b" + text + "\\b(?!([^<]+)?>))", "gim");
var e = document.getElementById("searchText").innerHTML;
var enew = e.replace(/(<span class='highlighted'>|<\/span>)/igm, "");
document.getElementById("searchText").innerHTML = enew;
var newe = enew.replace(query, "<span class='highlighted'>$1</span>");
document.getElementById("searchText").innerHTML = newe;
}
Check problem on : JSfiddle
Well, you are removing all </span> tags from the innerHTML in this line:
var enew = e.replace(/(<span class='highlighted'>|<\/span>)/igm, "");
And therefore also the </span> of .glyphicon. This is why the element becomes wrapped.
Btw: An exception is thrown: ReferenceError: highlightSearch is not defined

MDL animations stop working on copied <fieldset>

I am having an issue when cloning a section of my form (with jQuery).
The code below copies a 'fieldset', applies unique id's and for's, then appends the copy within the original 'fieldset'.
Although the copying works, all animations of the 'MDL'framework break on the copied 'fieldset'.
The code I am using is shown below:
//When add section is pressed
$('#add-section').click(function () {
// add 2 vars to count clicks per page
// get the fieldset's children that user is on, and clone it
var $fieldsetCopy = $('form>:nth-child(' + pageNumber + ')').clone();
// add '-2' to all id's of the cloned fieldset
$fieldsetCopy.find("*[id]").each(function () {
$(this).attr("id", $(this).attr("id") + "-2");
});
// add '-2' to all for's of the cloned fieldset
$fieldsetCopy.find("*[for]").each(function () {
$(this).attr("for", $(this).attr("for") + "-2");
});
// remove the first p element of the clone
$fieldsetCopy.find("p").first().remove();
// add the cloned fieldset within the original
$fieldsetCopy.appendTo('form>:nth-child(' + pageNumber + ')');
$()
});
I was guessing a problem be the JavaScript loading?
I ended up finding the answer myself. The problem was that the framework needed to be reloaded. To do this you can use the line:
componentHandler.upgradeAllRegistered();
Or, if you would like to target a specific element you can use:
componentHandler.upgradeElement(button);
The full documentation is here https://getmdl.io/started/#dynamic
Here is another example (without jQuery). Maybe it will help. Just rebuild the DOM manually.
<script type="text/javascript">
document.getElementById("addText").onclick = function () {
var form = document.getElementById("container_layout_text");
var newDiv = document.createElement("div");
newDiv.className = "mdl-textfield mdl-js-textfield mdl-textfield--floating-label desc_value";
var newInput = document.createElement("input");
newInput.className = "mdl-textfield__input";
newInput.type = "text";
var newLabel = document.createElement("label");
newLabel.className = "mdl-textfield__label";
newLabel.textContent = "kkk?";
newDiv.appendChild(newInput);
newDiv.appendChild(newLabel);
componentHandler.upgradeElement(newDiv);
form.appendChild(newDiv);
}

How to clone and place two different items in jquery in the respective positions

I have a div and a button to close that div. I am cloning them both separately and assigning a unique incrementing id to both. Everything is working in that the cloning happens and the button closes the Div.
I have a problem though regarding the placement of the button. I want the button to always be in the same position with reference to its cloned div. Any ideas how to do this?
HTML
X
<div id="id"></div>
Add
Javascript
function duplicateDiv(){
$('#btn').clone().attr('id', 'btn'+ cloneCount2++).insertBefore($('[id^=id]:first'));
$('#id').clone().attr('id', 'id'+ cloneCount++).insertAfter($('[id^=id]:first'));
//clearing the input items in the new cloned div
$("#id").find("input").val("");
}
function hideDiv(obj){
var currentId = $(obj).attr('id');
var divId = currentId.replace("btn", "id");
$("#"+divId).hide();
$("#"+currentId).hide();
}
If interpret Question correctly , try using .appendTo() , .insertAfter($("#btn" + cloneCount))
var cloneCount = 0, cloneCount2 = 0;
function duplicateDiv() {
//the close button
$("#btn")
.clone().attr("id", function(_, id) {
return id + (++cloneCount2)
})
.appendTo("body")
$("#id")
.clone().attr("id", function(_, id) {
return id + (++cloneCount)
})
.insertAfter($("#btn" + cloneCount))
//clearing the input items in the new cloned div
$("#id").find("input").val("");
}
function hideDiv(obj) {
var currentId = $(obj).attr('id');
var divId = currentId.replace("btn", "id");
$("#" + divId).hide();
$("#" + currentId).hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
X
<div id="id">div</div>
Add
One of the way is Jquery Offset
var destination = $('.original-content').offset(); // get position of original
$('.original-content').hide(); // hide original
$('.cloned-content').css({top: destination.top, left: destination.left}); // set position of cloned

Jquery - Toggle Image Src if Parent Class is Clicked Hide/Show

I have a program that is configured to hide/show table rows when a +/- icon is clicked. The functionality is working, however, I need to figure out a way to reset all hide/show icons when the parent category is toggled closed.
$(function(){
//src vars
var hide_src = "http://www.synchronizeddesigns.com/filter_hide.gif",
reveal_src = "http://www.synchronizeddesigns.com/filter_reveal.gif",
s = '';
//hide all sublevel elements
$(".subsub, .subsubsub").hide();
$("a").click(function(e){
e.preventDefault();
var tID = e.target.id,
tClass = '.' + tID.replace('HS', '');
$(tClass).toggle();
if(!$(tClass).is(':visible')){
s = hide_src;
//for each subcategory
$(tClass).each(function(){
//get class names into classes array
var classes = $(this).attr('class').split(' '),
parentClass = '';
//search classes array for class that begins with 'cat'
for (var j=0; j<classes.length; j++) {
if (classes[j].match("cat")){
parentClass = classes[j];
}
}
//find subsubsub elements that have a class that begins with 'cat#sub'
var subs = $('[class*=' + parentClass + 'sub]');
//if there are sub elements, hide them too
if(subs){
subs.hide();
/*****************************************************
NEED HELP HERE !!!!!!!!!!
Need a way to reset all hide/show images icon
when 'parentClass' hide/show is clicked to close.
*****************************************************/
}
});
} else {
s = reveal_src;
}
//Change image src
$("#" + tID).attr('src', s);
});
});
To replicate: Toggle open all parents and subs, then close one of the parents, then reopen the parent. You'll notice that the +/- icon remains in it's previous state
jsFiddle link
You can find the img nodes for the subcategories under the current one and then change their src attr.
I've updated your jsfiddle: http://jsfiddle.net/nTyWv/12/
The code could be something like:
if(subs){
innerIcons = jQuery.find("a > img[id*="+tID+"sub]");
if (innerIcons) {
$(innerIcons).attr('src', s);
};
subs.hide();
}

Categories

Resources