How to read an ID of a DIV, and put in a VAR?

Asked

Viewed 386 times

1

Friends,

On my page, with that DIV listed below, I would need to "read" the id that is on the tag < a id="memo-list-delete-6_2544_0" > that is to say the value "memo-list-delete-6_2544_0" and put in a variable to use later.. how to do this with JS ?

    <div id="memo-list-6">
        <i>Testo 00</i>
        <pre> Texto 01 </pre>
        <a id="memo-list-delete-6_2544_0" >
            <span class="UmaClasse">    </span>
        </a>
    </div>

I tried to make:

var MinhaInfo = document.querySelectorAll('#memo-list-6 a').length;

but it only returns a number...

  • You got more than one div where you need to do it?

  • Yes, close to 1000 Ivs....

  • Okay, can you explain more about your logical code? So you can get even more tips

  • Ola Sergio I will try, although I am new and have difficulty expressing myself! I also want to remind you that the issue has already been resolved, so from that point on the study is valid! I needed something fast and practical just for the display of the page. On this site.. the customer can upload an image to be analyzed, and make notes , and the technician can make notes as well. And then the reviewer.. and so on.. until the final work, at some point there are many Ivs with annotations, colors etc...but all with id with the same sequence "memo-list-xxx"

  • it turns out that some do not need to see the notes of others, which resolves with a display; None;... it turns out that doing u for/if made everything slow. but that code below was almost instantaneous.

1 answer

3


I believe that would be it Roberval:

var MinhaInfo = $('#memo-list-6 a').attr('id');

And just as a good practice tip, always try to use the standard of Omega for variables in any language you are programming. For javascript the default is camelCase.

Basically camelCase says that the first word of the variable will always start with a minuscule letter and from there the words start with capital letters. Examples:

var minhaInfo = 1;
var minhaVariavelManeira = 2;
var variavelPadraoCamelCase = 3;
var camelCase = 4;
  • Our was worth a lot! And I’ll already adopt those tips there! Brigadeo!

Browser other questions tagged

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