0

I have such a JS code :

var parent = titles[i].parentElement;
console.log(parent.innerHTML);

That prints this on console :

<title style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">MyTitle</title>

How can I change MyTitle to something else? Thanks.

EDIT :

My whole code is this:

var titles = document.getElementsByTagName('title');
for (var i = 0; i < titles.length; i++) {
    if (titles[i].innerHTML == 'MyTitle') {
        console.log(titles[i].innerHTML);
        var parent = titles[i].parentElement;
        console.log(parent.innerHTML);
        titles[i].innerHTML == '1215546162';
        console.log(titles[i].innerHTML);
        $(parent).attr('fill', 'blue');
    }
}
Anil
  • 3,722
  • 2
  • 24
  • 49
jason
  • 6,962
  • 36
  • 117
  • 198
  • 1
    `parent.innerText = "new"` or `$(parent).text("new")` – gurvinder372 Apr 21 '17 at 07:06
  • 1
    can you please share the html code? – brk Apr 21 '17 at 07:07
  • document.title = "new title", but why you want to change by javascript? – Anil Apr 21 '17 at 07:08
  • 6
    Wouldn't you just say `titles[i].innerHTML = "something else"`? If the inner HTML of the parent is exactly one element then clearly `titles[i]` *is* the element you want to update. – nnnnnn Apr 21 '17 at 07:10
  • 2
    Is this in an HTML document ? If so, how can you have a nodeList, array or whatever with more than a single `` element ? To modify its innerHTML, just do `document.title = 'your Text';`. Ah thanks for the edit, so it's an svg document. You may want to clarify it in your question directly, and show us a bit of this svg markup. – Kaiido Apr 21 '17 at 07:17
  • @nnnnnn I tried what you said but it didn't work. I edited my answer. – jason Apr 21 '17 at 07:20
  • 1
    What do you mean "didn't work"? Did you get an error, or did the wrong thing happen, or...? – nnnnnn Apr 21 '17 at 07:22
  • @nnnnnn MyTitle stayed the same, it didn't change. – jason Apr 21 '17 at 07:24
  • Possible duplicate of [how to change svg text tag using javascript innerHTML](http://stackoverflow.com/questions/4282108/how-to-change-svg-text-tag-using-javascript-innerhtml) – Anil Apr 21 '17 at 07:25
  • 1
    In your code you are doing `titles[i].innerHTML == '1215546162';` it should be `titles[i].innerHTML = '1215546162';` (single `=`) or even `titles[i].textContent = '1215546162';` for old browsers. – Kaiido Apr 21 '17 at 07:25

2 Answers2

3

Maybe this helps:

titles[i].innerHTML="New Title";

Title is changing (look at console) at this fiddle: https://jsfiddle.net/ek6f9jq8/2/

zuluk
  • 1,557
  • 8
  • 29
  • 49
  • I have updated the fiddle. My Suggestion works for me: https://jsfiddle.net/ek6f9jq8/2/ – zuluk Apr 21 '17 at 07:24
0

I don't know the the exact purpose of your code, but this can be helpful.

var head = document.getElementsByTagName("head")[0];
var title = head.getElementsByTagName("title")[0];
title.innerHTML = "Foo";
marmeladze
  • 6,468
  • 3
  • 24
  • 45