Selecting an element whose ID has a period in it [duplicate] - javascript

This question already has answers here:
Can't select div with id=":1"
(2 answers)
Closed 7 years ago.
I have an element that has a period in it:
<div id="parent">
<div id="my.element"></div>
</div>
and I want to select it using Prototype. I've tried this:
$$('#parent #my.element');
but it isn't working because it thinks the .element part is a class. Is there a way around this?
FYI, it isn't really an option to rename the element. Unfortunately I'm stuck with this naming scheme as well as only Prototype
Thanks

You can always escape your dot in css, so this should work
$$('#parent #my\\.element');
EDIT: As stated by Oriol and tested, you indeed have to use 2 slashes

Related

JavaScript - getting text from <strong> or div by class [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 2 years ago.
I wonder how to get data from this code. I want to download these numbers, but I don't know how.
I have already tried such methods but they did not work.
document.querySelector('.myclass');
document.querySelector('.myclass strong');
document.querySelectorAll('.myclass strong');
And this is the element from which I want to get the numbers. There are two ways on the site but I don't know which will be better so I send 2 items with numbers
<div class="myclass" role="button" tabindex="0">Next number: <strong>97554</strong></div>
or
<div class="anyclass">97532</div>
Did you mean
document.querySelector('.myClass').innerHTML;
document.querySelector('.myClass').innerText;
Use the data-* attribute to embed custom data. eg:
<div class='myClass' data-price="123"> Your ticket costs <i> 123 </i> </div>
Then you can use
document.querySelector('.myClass').getAttribute('data-price');
You need to use .innerText to get the numbers in the strong tag
document.querySelector('.myClass strong').innerText;

Javascript: Css-Selectors: (Bug or Feature?) Class-Name "starts-with" only selects element, when class is at first position in class-list [duplicate]

This question already has an answer here:
jQuery: Finding partial class name [duplicate]
(1 answer)
Closed 2 years ago.
I want to select an element using querySelector(...)
Let us assume to have a html-fragment like:
<div class="Class1_a123 Class2_z987">Div1 content</div>
<div class="Class1_a123">Div2 content</div>
I want to select only the first div, if it (and this is set dynamically) has the second class.
So I wrote the selector as follows:
document.querySelector("div[class^='Class2']");
The result: Nothing is selected.
So I wonder, whether there is a hierarchy, which class stands in front of the class list.
I thought it checks for all contained classes, whether they start with the expression I need.
Is there a fundamental mistake in my mind and/or understanding, how selectors are working?
If this is right, that the order of classes inside an elements classlist can be important for the layouting/styles of the web-site?
In this case I have to think about all code I've written in past, where I used the class^='...' selectors and have it to replace by class*='...' to let it work as expected.
Any suggestions/corrections are welcome.
So basically, you are trying to query element which has attribute "class" starts with "Class2". To make it works, you need to change the order of the classes.
<div class="Class2_z987 Class1_a123">Div1 content</div>
<div class="Class1_a123">Div2 content</div>

jQuery find closest of the given classes [duplicate]

This question already has answers here:
jQuery find which parent is closer?
(5 answers)
Closed 3 years ago.
I need to find the .closest() ancestor with any one of the given class.
jQuery("#gid").closest(".ui-tabs,.tab-pane,.jomsTabsContent");
What I want here, is which ever of the given classes is the closest one, I need to select that.
So for the following eg.
HTML
<div class="foo">
<div class="bar">
<div class="monkey">
<div id="gid">
<div>
<div>
<div>
<div>
JS
jQuery("#gid").closest(".foo,.bar,.monkey");
I want the above to select (only) the .monkey div.
PS: I know the above isn't correct syntax for what I'm 'looking for', but I am demonstrating what I am looking for.
Opps... It looks like, If there is an overlapping hirerchy, then then .closest() returns the closest of the provided CSS group.
Got it after a little google-foo here.

How to hide this button with JQuery [duplicate]

This question already has answers here:
How can I select an element with multiple classes in jQuery?
(14 answers)
Closed 6 years ago.
I am using jquery bootpag to display pagination on a page
<div class="textgray showing"></div>
How should I tell jQuery to select this div? I tried the following without but neither of these attempts work
$(".textgray .showing").html("some text");
$(".textgray showing").html("some text");
P.S I am a beginner in frontend so bear with me
The space means the second part is inside the first one (it's called the descendant combinator). Remove it:
$(".textgray.showing").html("some text");
You don't need to put that extra space in between selector. space in selector looks for descendant elements:
$(".textgray.showing").html("some text");

jQuery/Javascript find and replace all instances [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replace all instances of a pattern with regular expressions in Javascript / jQuery
How can I use jQuery to style /parts/ of all instances of a specific word?
Say I have the code:
<div class="replace">
<i>Blah</i>
<u>Blah</u>
</div>
Now, I want to replace all the < within the div with something else.
If I use $('#div').html().replace('<','somethingElse'); it only replaces the first instance.
How can I replace all the instances?
Thanks
Use regex
"anyString".replace(/</g,'somethingElse');
If you want to replace it inside the div then try this.
$('#div').html($('#div').html().replace(/</g,'somethingElse'));

Categories