Harshit
Harshit

Reputation: 1217

changes not reflected in JS

I am trying to setup a new "a" component using JS function which is called , using following :

function add_div(data){
    mydiv = document.getElementById("new_twt");
    var link =document.createElement("a");
    var text = "you have new conversations";
    link.setAttribute("name",text);
    link.onclick=function(){new_tweet(data);};
    mydiv.appendChild(link);
  }

Changes are not reflecting on the webpage , however if I use some other element such as button or new div it gets created instantly, am I missing something?

Upvotes: 1

Views: 327

Answers (2)

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28548

Try this:

var mydiv = document.getElementById("new_twt");
var aTag = document.createElement('a');
aTag.setAttribute('href',"yourlink.htm"); //or #
aTag.innerHTML = "you have new conversations";
aTag.onclick=function(){new_tweet(data);};
mydiv.appendChild(aTag);

Here is working JS Fiddle.

Upvotes: 1

Dziad Borowy
Dziad Borowy

Reputation: 12579

This works for me:

function add_div(data){
    var mydiv = document.getElementById("new_twt");
    var link = document.createElement("a");
    var text = "you have new conversations";
    link.name = text;
    link.href = '#';
    link.innerHTML = 'link';
    link.onclick=function(){ new_tweet(data); return false; };
    mydiv.appendChild(link);
}
  • I've added link text (innerHTML) so you can actually see the link
  • I've also added "href" so the link behaves as a link (with this you need to prevent default link action, like "return false" in the event listener, to prevent browser from jumping to the top)

Upvotes: 1

Related Questions