Add a tag to another tag using Javascript

Asked

Viewed 1,382 times

1

I have a jQuery that I use on a website that I’m doing that applies the zoom effect on an image. Only when I hover the mouse over the image and it zooms in, jQuery creates another IMG tag with the same image by applying a class="zoomImg" to it:

Only I’m wanting to map this image, so I used a Javascript code in which when hovering over this image that is inside a SPAN with an id="Ex1", it creates a MAP tag in this SPAN:

var para = document.createElement('map');
para.setAttribute('name', 'crateria-map');
document.getElementById('ex1').appendChild(para);
mostra = function() { /* Não faz nada */ } ---> este código eu obtive em outra pergunta, pois a função estava fazendo a tag ser criada toda vez que eu passava o mouse na imagem.

Since I’m going to map this image, I need to add a SPAN tag inside this SPAN tag that was created and using the same code I’m not getting:

var sub = document.createElement('area');
sub.setAttribute('class', 'html5lightbox');
document.getElementByName('crateria-map').appendChild(sub); 

In the MAP tag I used the id="Ex1" of the span to add the tag, so I thought I’d use the name of the SPAN tag to add the AREA tag, but as I said, it didn’t work, I’m using a "Function shows()" to enable Javascript.

I hope I have been clear and if I am not being, I apologize, because I understand almost nothing of Javascript.

  • Ever tried to give a para.appendChild(sub); and then give the father an append to?

  • 1

    There is no getElementByName only plural and gives an array... forehead document.getElementsByName('crateria-map')[0]

  • Do you have those lines in the same scope? In that case it would be enough para.appendChild(sub); instead of document.getElementByName('crateria-map').appendChild(sub);

  • Felipe, it seems that it worked out your answer Document.getElementsByName('crateria-map')[0] but I will check if everything is all right even.

  • Felipe, it worked, thanks for the help, it would be good for you to put this information as an answer so I can mark it, but vlw!

  • @Marcelolinsdecarvalho in this your comment referred to my solution with the name "Felipe", I am Sergio. Which of the ideas used?

Show 1 more comment

1 answer

0


In Javascript does not exist getElementByName only plural getElementsByName and will return an array.

So if there is only one element you can fetch the first element of that array with document.getElementsByName('crateria-map')[0].appendChild(sub);.

Looking at your code, I wonder if those lines are in the same position. 'Cause then it gets simpler, you don’t have to look in the DOM and you can use only para.appendChild(sub);. The whole code would be:

var para = document.createElement('map');
para.setAttribute('name', 'crateria-map');
document.getElementById('ex1').appendChild(para);

// assumindo que estas linhas estão no mesmo escopo/função
var sub = document.createElement('area');
sub.setAttribute('class', 'html5lightbox');
para.appendChild(sub);
  • are in the same function yes Die, but I am checking the answer of Felipe, seeing if everything is all right here, but I will try this way too, thank you

  • Sergio, for now I will keep the solution of Felipe, but anything if you think better I modify for yours, thanks also for the help.

  • 1

    kkkkk, it was you who sent, sorry, I mark it, I ended up getting confused...

Browser other questions tagged

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