Correct syntax of document.getElementById [duplicate]

1

Reviewing my code, I realized that I have different syntax from document.getelementbyid:

   document.getElementById("prcavance").value = value ;
    document.getElementById('up_file').disabled = true;

I have it with double quotes and another with single quotes, what is the correct form?

Apparently it works in both ways ..

    
asked by Richard 24.05.2018 в 21:59
source

3 answers

1

Complementing what my colleagues have commented on that there are no differences between double and single quotes, I explain the following:

  

It is recommended to use one of the two types of quotes and be   recurrent in your code with it. If possible, do not use both   mixed types in javascript.

Why? because generally you will not use javascript as the only language even in cases like combining angularjs + nodejs requires a third factor like HTML.

  

So if you need to embed javascript in another language (like PHP   for example) or vice versa will be very difficult:

     

Detect what a comment is and what it is not. Quickly observe the   variables in the code in order to debug it Determine where the term ends   code of one language to start the other

In short, code maintenance will be complex, the code will be a bit illegible and also likely to generate more errors when executing it.

For example, I use the single quotes as much as possible for javascript and so I leave the doubles for HTML and PHP, so when I embed javascript in my HTML or PHP it is more practical and simple.

A didactic example is the following:

<?php echo "<script>alert('Hola')</script>"; ?>

How would you do to execute the previous statement if you both put them in double quotes? It would be a bit complicated.

Another example:

var input = '<input type="text" name="prueba" id="prueba" >';

Notes how easy it is to embed HTML in javascript respecting the use of single and double quotes for each of them?

Observation: There are some cases where using javascript with double quotes is more feasible than using it with single quotes. The important thing is to try not to mix both for the same language as mentioned above.

    
answered by 24.05.2018 / 23:42
source
1

No matter how you do it, JavaScript will recognize as a text string any element that is inside "" or "" Everything depends on you and your programming style.

I particularly use double quotes, since in other programming languages, single quotes are only for one character.

    
answered by 24.05.2018 в 22:05
0

Here is an answer with the difference between using single and double quotes: link

    
answered by 24.05.2018 в 22:01