-1

Javascript

var1.attr('title',vartit);
$(var1).attr('datetime',vartim);

Html

<time title="" datetime=""> </time>

I could successfully add the variable value in the respective html attributes but its not visible in view source.

I found some answers like innerHtml and all but they are for inside of time or div but not for html tag attributes, how can make that possible??

Thanks in advance

stacky
  • 311
  • 1
  • 6
  • 26
  • @Downvoter, care to explain the reason? – stacky Jun 10 '17 at 14:58
  • 3
    The "View source" facility just shows the source as it was at the time the page was initially built. The browser debugger tools however generally will show you the elements in their current condition. – Pointy Jun 10 '17 at 14:58
  • Did you check that the `vartit` and `vartim` variables have correct value? – Botimoo Jun 10 '17 at 14:59
  • @stacky .can you post the working level.above code is not clear to understand your problem – prasanth Jun 10 '17 at 14:59
  • @Botimoo Yes, they have correct value. – stacky Jun 10 '17 at 15:00
  • 1
    Why do you want the change to show in "view source"? – JJJ Jun 10 '17 at 15:01
  • in firefox you can select the whole page, right click then view election source - other browsers may do the same – Jaromanda X Jun 10 '17 at 15:01
  • Read the comment from @Pointy, View source only displays the source code of the initial load and not with any js amends (dom changes are not shown) – Web pundit Jun 10 '17 at 15:01
  • Possible duplicate of [Content of html page changed by jQuery but "View Source" don't reflect the changes](https://stackoverflow.com/questions/9654737/content-of-html-page-changed-by-jquery-but-view-source-dont-reflect-the-chang) – JJJ Jun 10 '17 at 15:02
  • When am checking the validation in w3.org its giving me an invalid datetime format, as its empty – stacky Jun 10 '17 at 15:03
  • of course it is, because it's not there in the raw source – Jaromanda X Jun 10 '17 at 15:04
  • So what you really should be asking is "how to get the current HTML of a web page." If you're on Chrome, right click anywhere, select "Inspect element", right click on the tag, choose "Copy -> Copy outerHTML". – JJJ Jun 10 '17 at 15:06
  • Tried with fetch as google, its the same title and datetime are empty, will that wont impact on seo?? – stacky Jun 10 '17 at 15:19

2 Answers2

1

I don't know the whole context of the snippet you shared. But here you can find a working demo

The JavaScript part is just:

$(_ => {
 $('h1').attr('datetime', '27-03-2017');
});

Then just look at the console to see the attribute.

jramirez
  • 155
  • 9
1

If you want to use a jquery selector gor your "time" tag, it should be something like $('time').attr("yourattr",value). Try this and let me know if this works

Rohit
  • 96
  • 1
  • 7