Javascript image link does not work

Asked

Viewed 193 times

1

Okay, I know there are thousands of ways to do this(better and easier) but I’d just like to make it work. I am passing the images through the JS and would like to assign the link of the same, however, não funciona... The console does not return any error, someone could help ?

window.onload = function(){
    rdSocial = $("#social");

    var fb = '<img src="img/fb.png" value="1" class="img" onclick="redireciona(this);">';
    var twt = '<img src="img/twitter.png" value="2" class="img" onclick="redireciona(this);">';
    var insta = '<img src="img/insta.png" value="3" class="img" onclick="redireciona(this);">';

    rdSocial.innerHTML += fb;
    rdSocial.innerHTML += twt;
    rdSocial.innerHTML += insta;
}

function redireciona(obj){
    if(obj.value == "1"){
        window.location="#";
    }
}
  • 1

    I believe the attribution does not work because your social is a Jquery object; if you change rdSocial.innerHTML += fb; for rdSocial.html(fb); or change your rdSocial = $("#social"); for document.getElementbyID('social'); will work....

1 answer

2


You’re mixing up some concepts:

  • jQuery and native Javascript

Use $('#social') will give you a jQuery object and not a DOM element where you can use it .innerHTML. Or seedlings to native using .getElementById(id) or switch to jQuery and use .html(conteudo)

  • value is not an image property

The element <img> It is not by nature value. So it’s read as an attribute and to get it you have to use img.getAttribute('value').

Tip: Do it with native code:

window.onload = function(){
    rdSocial = document.getElementById("social");

    var fb = '<img src="img/fb.png" value="1" class="img" onclick="redireciona(this);">';
    var twt = '<img src="img/twitter.png" value="2" class="img" onclick="redireciona(this);">';
    var insta = '<img src="img/insta.png" value="3" class="img" onclick="redireciona(this);">';

    rdSocial.innerHTML += fb;
    rdSocial.innerHTML += twt;
    rdSocial.innerHTML += insta;
}

function redireciona(el){
    var value = el.getAttribute('value');
    if(value == "1"){
        window.location="#";
    }
}
  • 1

    It worked, thanks !

  • 1

    @Gabrielozzy I’m glad Sergio’s answer helped you! I was looking at your profile, and I noticed that you haven’t given any votes on the site yet (except for the acceptance). I suggest you revise your questions and vote on other answers that may have helped you (you can also vote on the ones you accepted). If you find questions from other people that seem useful to you, vote for them too! The site is in need of more votes, they are fundamental to the organization of the content, to separate the wheat from the chaff. Thank you!

  • 1

    Thanks for the tip @bfavaretto ! I didn’t pay attention to that, and we are in a community where we have to help to be helped, always ! To be one more to point a good way is always good ! I will follow the advice ! Thanks for the touch !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.