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

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

Related

Searching for a specific word in JavaScript without using includes() method [duplicate]

This question already has answers here:
includes() not working in IE
(3 answers)
Closed last month.
I am required to find for a specific word in a text input whereby the word is the "+" symbol. The users for my program uses IE8 and IE11 and unfortunately when I looked in w3schools, I found out that the includes() method is not supported in Internet Explorer 11 (or earlier). Below is my code:
<input type="text" class="recycle" name="txtEXT_AK" id="txtEXT_AK" size="10" onclick="this.select()">
document.getElementsByClassName("recycle").onkeyup = function(){checker()}
function checker(){
var text_value=document.getElementsByClassName("recycle");
word="+";
var textValue = text_value.value;
if (textValue.includes(word))
{
alert('found')
}
}
So far, there is no alert() coming out when I tried to enter the "+" symbol into the text box. Is there any alternative for the includes() method that I can use for the checker code? I have tried indexOf, contains and match but so far there is no luck. Any help is really appreciated!
Actually,the key bug is not "includes", you should change getElementsByClassName('recycle') to getElementById('txtEXT_AK')

Simple format of text on front-end of my website (replace characters)? [duplicate]

This question already has answers here:
Remove first character from a string if it is a comma
(4 answers)
Closed 3 years ago.
My website uses product references in this style: "R202020"
I want them to be shown like this for the users of my website: "BA2202020"
So basically I'm looking for a script, which formats the style of my reference numbers (should affect a ".reference" class I've created) by:
Removing the "R" in the original reference - replacing it with a "BA2" in stead - leaving the rest as it is (the "202020" part).
How can I do this?
Find 1st character of your string using string[0] and replace that with your desire value like below.
var string=$('.YourClass').text();
var result = string.replace(string[0],'BA2');
$('.YourClass').text(result);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class='YourClass'>R202020</span>
Try replace method. https://www.w3schools.com/jsref/jsref_replace.asp
'R202020'.replace('R2','BA2') // BA202020

Get Element with Hyphen in Javascript [duplicate]

This question already has answers here:
How can I get the data-id attribute?
(16 answers)
Closed 6 years ago.
HTML snippet:
<input title="List" data-id="1698481">
In IE 11 console, I've been trying various commands, and everything without a hyphen comes back correctly until I hit "data-id."
document.getElementsByTagName("input")[0].title
"List"
document.getElementsByTagName("input")[0].data-id
'id' is undefined
As per other threads on this subject, I tried other syntaxes (camel case, etc.), but I still can't get it to return any value
document.getElementsByTagName("input")[0].dataId
undefined
document.getElementsByTagName("input")[0].["data-id"]
Expected identifier
document.getElementsByTagName("input")[0].['data-id']
Expected identifier
Any assistance would be appreciated.
Use .getAttribute():
document.getElementsByTagName("input")[0].getAttribute("data-id")
The data-* attributes are special:
document.getElementsByTagName("input")[0].dataset.id
data-* attributes are converted from hyphens to camelCasing, so a data-test-attribute="test" would be equivalent to:
htmlElement.dataset.testAttribute; // test
For more information, see the MDN on dataset.

How can I change the text using JavaScript [duplicate]

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.

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