Get Element with Hyphen in Javascript [duplicate] - javascript

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.

Related

Parse value only from JSON string [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 months ago.
An API returns a string of text that looks like this (xxx used for security):
{"xxx":{"xxx":{"xxx":{"xxx":{"results":[{"latest.GigabytesIngested":12641.824682336}]}}}}}
If I do this:
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]);
I get this, which is fine:
{ 'latest.GigabytesIngested': 12641.82487968 }
My problem is I only want to grab the number. The below attempt doesn't work, maybe because there's a dot in the key name, or maybe because I'm just doing it wrong?
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0].latest.GigabytesIngested);
#derpirscher answered correctly in a comment:
console.log(JSON.parse(body).data.actor.account.nrql.results[0]['latest.GigabytesIngested']);
Yes, the period in the key is the problem. You need to use an alternate way to reference the key.
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]["latest.GigabytesIngested"]);
or
var result = JSON.parse(body).xxx.xxx.xxx.xxx.results[0];
var lgi = result["latest.GigabytesIngested"];
console.log(lgi);

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

How to read data between xml tags with attributes in javascript/ionic without using DOM/ActiveXObject? [duplicate]

This question already has answers here:
Parse XML using JavaScript [duplicate]
(2 answers)
Closed 5 years ago.
I simply need to extract some info between two tags, in this case, <wsse:BinarySecurityToken>
For example:
<wsse:BinarySecurityToken att1="abc" att2="cd3" att3="adfa">This is a text I need!!!===</wsse:BinarySecurityToken>
I tried
match = text.match(/<wsse:BinarySecurityToken[.*]>([^<]*)<\/wsse:BinarySecurityToken>/g)
does't work!
Or is there anything better than regex? I use angularJs 1
You want to do:
match = text.match(/<tag1.*>([^<]*)<\/tag1>/g)

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 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

Categories