Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trigger an event when this span value changes?

I'm trying to get a console log when the price of the current page's stock changes.

page: https://www.google.com/finance?q=NASDAQ%3AGOOG&ei=yvbRVbHSCcKgmAGyu7CoDA

Element ID: #price-panel > div > span > span

Attempt 1: fail

$("#price-panel div span span").onchange = function() { console.log('change')}

Attempt 2: fail

document.getElementById('price-panel').children[0].children[0].onchange = function() { console.log('change')}

builtwith.com says the javascript is the "google api" which is ambiguous to google searches so that didn't help much.

What event can I watch to find out when this element is change?

like image 959
Jacksonkr Avatar asked Aug 17 '15 15:08

Jacksonkr


People also ask

How do I change the value of a span element?

Use the textContent property to change the text of a span element, e.g. span. textContent = 'Replacement text' . The textContent property will set the text of the span to the provided string, replacing any of the existing content.

Can span tag have value attribute?

span elements do not have a value property. Instead, use html() for the HTML or text() for the text nodes.

How do I add a value to a span tag?

//set value in span using jquery $("#idname"). text("your value"); //set value in span using Javascript document. getElementById("idname"). textContent="Your value"; document.


1 Answers

From Dom Events as your try to listen to text changes in <span> you may be able to use DOMCharacterDataModified.

Or you can use MutationObserver to capture the changes.

var span = document.querySelector('#test');
var text = document.createTextNode('tttt');

// This may be deprecated.
span.addEventListener('DOMCharacterDataModified', function() {
    alert('span text changed to: ' + this.innerHTML);
});

// You may use MutationObserver instead.
var mutateObserver = new MutationObserver(function(records) {
  console.log(records);
});
mutateObserver.observe(span, {
  childList: true,                                 // capture child add/remove on target element.
  characterData: true,                     // capture text changes on target element
  subtree: true,                                   // capture childs changes too
  characterDataOldValue: true  // keep of prev value
});

setTimeout(function() {
  span.innerHTML ='SSSS';
}, 2000);

setTimeout(function() {
  span.appendChild(text);
}, 3000);

setTimeout(function() {
  text.data = '3123';
}, 4000);
<span id="test">This is a span</span>
like image 163
fuyushimoya Avatar answered Nov 03 '22 08:11

fuyushimoya