How can I change the text using JavaScript [duplicate] - javascript

This question already has answers here:
How do I change the text of a span element using JavaScript?
(18 answers)
Closed 7 years ago.
<div class="basePrice">$99.99</div>
I want to change the value $99.99 using JavaScript.
I tried using getElementByClass but it didn't produce the results that I was hoping to get.

document.getElementsByClass returns a NodeList, which is kind of like an array.
You have to specify which element (there's only one here, so I'm assuming the first) you're looking for, and then you can use .textContent to change the text of the node.
document.getElementsByClassName("basePrice")[0].textContent = "$49.99";
document.getElementsByClassName("basePrice")[0].textContent = "$49.99";
<div class="basePrice">$99.99</div>

You can do that like so
document.querySelector('.basePrice').innerHTML = 'different text'
FIDDLE

try getElementByClassName I believe is the proper syntax.

Related

Is there a way in plain javascript to select element in options by value? [duplicate]

This question already has answers here:
Find an element in DOM based on an attribute value
(11 answers)
Closed 6 years ago.
As you can see from the title, I am wondering if there is a way in plain javascript to select an element from option fields by some value. I know I can do it with jQuery like this - option[value="'+ myObject.value+'"] but I want to know if I can do the same with javascript without using some loop to find the index and then select it by its value.
P.S. I guess jQuery is doing exactly this^ but I am not sure.
You can use querySelector with css attribute equals selector.
var ele = document.querySelector('option[value="' + myObject.value+ '"]');
You can do it as easily in plain JavaScript by using document.querySelectorAll:
document.querySelectorAll("option[value='" + myObject.value + "']");

How to combine multiple find() in jquery? [duplicate]

This question already has answers here:
jquery multiple attribute selector problem
(3 answers)
Closed 6 years ago.
I have these two conditions:
1) Find all div with a "data-filter-program" attribute which equal to something
2) Find all div with a "data-filter-expertise" attribute which equal to something
And I need to somehow combine these two into one statement in jquery. I would like something like this:
mentors = $("div").find("[data-filter-program*='"+selected_program+"']" && "[data-filter-expertise*='"+selected_expertise+"']");
How do I actually achieve this correctly in Jquery?
Thanks!
mentors = $("div").find("[data-filter-program*='"+selected_program+"'], [data-filter-expertise*='"+selected_expertise+"']");
Just add a comma between each item you would like to find.
$("div").find("#apples, #bananas, .grapes, div");

object.style.x doesn't return anything [duplicate]

This question already has answers here:
Get a CSS value with JavaScript
(8 answers)
Closed 8 years ago.
In JavaScript on my website I have something like this:
console.log(document.getElementById("side_news").style.display);
and I have tried this with a lot of styles and it doesn't return anything, just blank. What am I doing wrong?
Try using getComputedStyle():
var sideNewsDiv = document.getElementById('side_news');
getComputedStyle(sideNewsDiv).getPropertyValue("display");
MDN documentation: Window.getComputedStyle().
Most elements don't show all of their attributes when accessing through object.style. A div element has a default display style of block but accessing it through style will result an empty value.
A solution is to use getComputedStyle - or, if not supported by the browser, currentStyle.
if (window.getComputedStyle)
status = window.getComputedStyle(targetElement, null);
else
status = targetElement.currentStyle;
This will show the element's style with all the css changes.

How to get the value from HTML code using javascript? [duplicate]

This question already has answers here:
How To get values in Span Tag from JS
(6 answers)
Closed 9 years ago.
My js code are given belowbelow:
var intime = document.getElementById('clocktime').innerHTML;
using this I am getting the value of intime is below :
<span class="clocktime">07:25:41 PM</span>
I just want the value like 07:25:41 PM from the code.How can I get this value using js?Any Idea?
This works in IE9+
document.querySelector('.clocktime').textContent;
Here’s an example: http://jsfiddle.net/bpWP4/
As T.J. Crowder pointed out, you can use innerHTML to make it work in IE8+.
document.querySelector('.clocktime').innerHTML;
document.getElementsByClassName('clocktime')[0].innerHTML;
If you want IE 8 compatibility:
document.querySelector('.clocktime')[0].innerHtml; // Take note of the added period in the selector.
Both of these samples use a function that returns a array of elements matching the selector. That's why I use [0] to get the first element from those arrays.
You could grab the <span> instead of the element that surrounds it. For example:
var intime = document.querySelectorAll('#clocktime .clocktime')[0].innerHTML;
This assumes there's only one <span class="clocktime"> inside the element with id="clocktime".
But note that querySelectorAll isn't supported in IE 7, or really old versions of Firefox and Opera:
http://caniuse.com/#search=queryselectorall

How to accept any characters in between * of an getElementById (stack_ * _overflow)? [duplicate]

This question already has answers here:
jQuery selector regular expressions
(10 answers)
Selecting element which starts with "abc" and ends with "xyz"
(3 answers)
Closed 9 years ago.
I have a page which changes the ID of input fields every time. So for example if I visit the page now, the ID can be "stack_15_overflow" and next time it can be "stack_293_overflow".
I want to use a wildcard value for getElementById, such as "stack_ * _overflow" (where * matches anything), to get that value related to any input field starting and ending with some specific text, no matter what text is in between.
Code:
function HelloId(string){
var name=string
document.getElementById('stack_*_overflow').value=name;
}
Using jQuery's attribute starts with and attribute ends with selectors:
$("[id^='stack'][id$=overflow]");
Note that these selectors are expensive, specifying type of the element can improve the performance:
$('element').filter("[id^='stack'][id$=overflow]");
var elements = document.querySelectorAll('[id^="stack_"][id$="_overflow"]');
You can't achieve this by using getElementById.
A better solution would be querySelector or querySelectorAll, where you get full support for CSS selectors.
Reference at MDN
You will need 2 of these attribute selectors:
Get elements that start with a specific string
document.querySelector('[id^="stack_"]');
Get elements that contain a specific string
document.querySelector('[id*="stack_"]');
Get elements that ends with a specific string
document.querySelector('[id$="_overflow"]');
By combining ends and starts selectors, you get the following and are able to achieve your desired result:
document.querySelector('[id^="stack_"][id$="_overflow"]');
Happy coding!
Code:
$("body:regex(id, stack_[0-9]+_overflow)");
using this plugin

Categories