Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I want to hide by the class and the name attribute in div
This wont work
$("div.ind_post").find("[name^='1']").hide();
This work
$("div.ind_post").hide();
$("div").find("[name^='1']").hide();
I wonder why i can hide by the element with class, or the name, but i cannot hide with class and name both.
so my question is how to hide within the class, by a specify name, thanks!
find searches descendants
Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.
You should do
$("div.ind_post[name^='1']").hide();
I suspect the reason why $("div").find("[name^='1']").hide(); worked is that you had some div element higher up in the DOM hierarchy
Keeping in mind that name attributes are not allowed on div elements.
find searches the descendants of the selected elements.
If you want an element which matches a class selector and an attribute selector then you need to either search for them together in the first place:
$("div.ind_post[name^='1']")
or filter the collection of matched elements on the extra rule
$("div.ind_post").filter("[name^='1']").hide();
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to find the appropriate way to edit an element but it seems like their is only one node with an ID. I have thought of
my code looks like:
var root=document.getElementById("app-mount").childNodes;;
var child = root[n].innerHTML;
But this is not reusable to get the path to any element such as $0 used in chrome dev tools. I was wondering if there was a method one could call on $0 to just give me the path so one could know how to target it as one does for an ID document.getElementById('id');
Edit:
after getting help I have updated my code to look like:
document.querySelectorAll('svg')[1].outerHTML="<img id='orb' class='orb' src='https://i.imgur.com/k3d8qMN.gif' width='50' height='60'>"
Its for a theme I am making for discord!
Thanks for the help!
I am not sure that I am following your question very well, but if I understand you correctly, you are looking for something like querySelector or querySelectorAll.
You can use CSS commands to target various HTML elements. eg:
document.querySelector('div'); //returns the first div
document.body.querySelectorAll('div'); //returns all the divs attached to the body element
You can also target ids:
document.querySelector('#app-mount');
or classes:
document.querySelector('.blue');
and query selectors may also be used:
document.querySelector('#app-mount > ul > li:nth-child(3)');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how can I add span that have arrow picture for the ul.menu li that have child elements (ul.sub-menu li ).
HTML
<ul class="menu">
<li>hhh</li>
<li>hhh
<ul class="submenu">
<li>kkk</li>
<li>kkk</li>
<li>kkk</li>
</ul>
</li>
<li>hhh</li>
</ul>
how can do that by jquery or css
This jQuery code should work:
$(".submenu").parent().append("<span class='arrow'></span>");
There is no proper way exist to give you perfect answer, because you don't provide complete code (JS, HTML).
But as your requirement: if you want to add a span to parent menu -
First you need to select parent element, for this you can use .parent() jquery selector.
Jquery selector .parent() : - Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
Here complete documentation of jquery .Parent()
And second:
use .append() - for adding element to the end of selected element
$(".submenu").parent().append("<span class='arrow'></span>");
use .prepend() - for adding element to the beginning of selected element
$(".submenu").parent().prepend("<span class='arrow'></span>");
Try this,
$("ul.submenu").closest('li').children('a').append("<span> ▼</span>");
Demo
Use this if you want add class to ul tag along with menu
$(".submenu").parent().parent().append("<span class='arrow'></span>");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I need to get the element by class of "balls" from the div gameContent.
Basically grabbing the lottery numbers from Play4 from this site:
http://www.flalottery.com/play4.do
How can I get the element by class from another class? If I just do balls, all of the numbers show up, which aren't relevant and would mess up data.
Do you mean something like this:
document.getElementsByClassName('gameContent')[0].getElementsByClassName('balls')
Get elements by class "gameContent" followed by "balls". Query assumes that the first gameContent is what we are interested in.
Hope this helps.
you can use the following query selector
var elems = document.querySelectorAll(".gameContent .balls")
That is pure JavaScript. You can of course use the same query selector for jQuery
For instance with jQuery this would be
var elems = $(".gameContent .balls")
Notice how the query selector is identical.
Did you try
$(".gameContent .balls")
Judgeing by the page you've included in your question, you'll probably want to iterate through the <span> elements to get each ball number:
$('.gameContent .balls').each(function(){
alert('next ball: '+$(this).html())
});
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Suppose the following markup:
<body>
link [ some text here
link [ some more text here
</body>
Is there anyway to use jQuery to remove the ' [ ' from the top line but not the bottom one?
Note: I don't have the access to the markup, but I can add elements, divs etc. using jQuery if I wanted to. BUT jQuery does not need to target the string of ' ] ' in particular - it can be something like "remove next 3 characters after uniqueLink1.
jQuery doesn't really help much with manipulating text nodes, but here it is:
var tn = $('a[name="uniqueLink1"]')[0].nextSibling;
tn.nodeValue = tn.nodeValue.replace('[', '');
Demo
$()[n] is a shorthand for $().get(n), so [0] will return a reference to the first matched DOM element inside the jQuery object, the uniqueLink1 anchor.
nextSibling, as the name implies, grabs the next sibling node. In this case, the text node that follows the given anchor element.
nodeValue gets and sets the content of the text node.
String.replace() given a string as the first argument replaces only the first occurrence, thus this should be enough enough given the DOM structure is similar to the posted one.
This will filter the textnodes, and remove a [ from the first on it finds:
var textNode = $('body').contents().filter(function() {
return this.nodeType == 3;
}).eq(1);
textNode.replaceWith(document.createTextNode(textNode[0].textContent.replace('[','')));
Example fiddle
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to add an active class to the <li> that an ID is in, for example:
<li>text</li>
How can I add a class to the <li> that #foo is in?
$('#foo').parent('li').addClass("yourclass")
$("#foo").closest("li").addClass("bar");
If your <a> tag might be wrapped in other elements, and not a direct child of <li>, parent('li') will not work:
http://jsfiddle.net/PMfVK/
HTML
<ul>
<li><div><a id="foo">Foo</a></div></li>
<li><div><a id="bar">Bar</a></div></li>
</ul>
JS
$("#foo").closest("li").addClass("foo"); // this will correctly grab the li
$("#bar").parent("li").addClass("bar"); // this will grab nothing
$('#foo').parent('li').addClass('active');
$("li").has("#foo").addClass("yourclass")
$('a[id]').closest('li').addClass('active');
demo
First select the element with the ID foo using the ID selector. Then, use .parents to select the <li> parent of that element. Use .eq(0) to ensure that only the nearest parent is found. Finally, use addClass to add the class to the <li> element.
Since we are using parent s even if the element with the foo ID is nested several elements below the <li> it will still be selected.
$("#foo").parents("li").eq(0).addClass("bar");
See my example on jsFiddle.