HTML - Problemas con DataSet HTML5

 
Vista:
sin imagen de perfil

Problemas con DataSet HTML5

Publicado por Victor (22 intervenciones) el 08/08/2017 10:23:18
Buenos dias.


Tengo estoy intentando acceder al getAttribute de un obleto html pero me dice el navegador que esa propiedad no existe.
Este es el codigo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
            var plant = document.getElementById("stranwerryPlant");
            var fruitCount = plant.getAttribute("data-fruit");
            console.log(fruitCount);
        </script>
    </head>
    <body>
        <div>Ejemplo1 DataSet,DataAtributes y JavaScript</div>
        <ul id="vegetable-seeds">
            <li data-spacing="10cm" data-sowing-time="March to June">Carrots</li>
            <li data-spacing="30cm" data-sowing-time="Febrery to March">Celery</li>
            <li data-spacing="3cm" data-sowing-time="March to September">Radishes</li>
        </ul>
        <div id="stranwerryPlant" data-fruit="12">EEeooooooo</div>
    </body>
</html>

Alguna idea?

Saludos.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 1.144
Oro
Ha mantenido su posición en HTML (en relación al último mes)
Gráfica de HTML

Problemas con DataSet HTML5

Publicado por xve (1543 intervenciones) el 08/08/2017 16:32:23
Hola Victor, el código de javascript, no lo puedes añadir antes que el codigo html al que hace referencia... de todas maneras, te recomiendo utilizar el dataset... algo así:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html>
	<head>
		<title>TODO supply a title</title>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
	</head>
	<body>
		<div>Ejemplo1 DataSet,DataAtributes y JavaScript</div>
		<ul id="vegetable-seeds">
			<li data-spacing="10cm" data-sowing-time="March to June">Carrots</li>
			<li data-spacing="30cm" data-sowing-time="Febrery to March">Celery</li>
			<li data-spacing="3cm" data-sowing-time="March to September">Radishes</li>
		</ul>
		<div id="stranwerryPlant" data-fruit="12">EEeooooooo</div>
	</body>
	<script>
		var plant = document.getElementById("stranwerryPlant");
		var fruitCount = plant.dataset.fruit;
		console.log(fruitCount);
	</script>
</html>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problemas con DataSet HTML5

Publicado por Victor (22 intervenciones) el 09/08/2017 07:29:27
Muchisimas gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar