I have...">

Additional Div class options for jQuery

I have a class like this:

<div class="photo" parameter1="custom" parameter2="custom"></div>

I have only 4 parameters.

I need a way to present these parameters inside my photo div, which will be used for jQuery code, and still have my W3C compatible site. Any ideas?

+3
source share
4 answers

Set doctype in HTML5 and use custom data- * attributes .

<!DOCTYPE HTML>    
...
<div class="photo" data-parameter1="custom" data-parameter2="custom" data-parameter3="custom" data-parameter4="custom"></div>
+12
source

If you use HTML5, you have the option to create custom attributes with a prefixdata-* , which allows you to use something like

<div class="photo" data-parameter1="custom" data-parameter2="custom">...</div>
+4
source

, sysntax , div

, w3c, .

var a=$('.photo').attr('parameter1');
var b=$('.photo').attr('parameter2');
var c=$('.photo').attr('parameter3');
var d=$('.photo').attr('parameter4');

and if you want to add new attributes using jquery, you can do it like this:

$('.photo').attr('newParameter','value');

Hope this works well for u

+1
source

I think you disagree with some of the concepts in HTML.

Firstly, a div is not a "class", you have a "div element". A div element may have a class attribute.

Secondly, you should use </div>to close your element, not </class>.

For the problem, another solution is to use a hidden element inside your div, e.g.

<div class="photo">
  <span class="hidden" name="parameter1">...</span>
  <span class="hidden" name="parameter2">...</span>
  <span class="hidden" name="parameter3">...</span>
  <span class="hidden" name="parameter4">...</span>
</div>

with css style as follows:

.hidden {
  display: none;
}

and finally jQuery for extracting values:

var value = $('div.photo span[name="parameter1"]').html();
+1
source

Source: https://habr.com/ru/post/1795038/


All Articles