custom-data-attribute

Are empty HTML5 data attributes valid?

元气小坏坏 提交于 2019-11-26 16:06:58
I'd like to write a simple jQuery plugin that displays inline modals under specified elements. My idea is for the script to auto-init based on data attributes specified on elements. A very basic example: <p data-modal-target>Hover over me for an inline modal!</p> <div data-modal-content data-modal-align="right" data-modal-trigger="hover" data-modal-offset="10px"><!-- any desired syntax can go here --></div> I'm just wondering if data-modal-target in the above example is valid, or does it have to be data-modal-target="true" ? I don't care about anything crappier than IE9 etc, my only

JQuery .data() not working?

寵の児 提交于 2019-11-26 15:51:39
问题 Recently I was coding away, and I ran into a weird issue. I was attempting to assign a data attribute to a new element I had created (via jQuery), only to discover it wouldn't actually assign the attribute. See the link below for an example, the code is listed below: http://jsfiddle.net/y95p100c/1/ Any idea why this is happening? I've never stumbled into this... var div = $("<div />") $(div).data("foo", "bar") console.log($(div)[0].outerHTML) // prints <div></div> 回答1: data doesn't set data-*

How to set data attributes in HTML elements

我的未来我决定 提交于 2019-11-26 15:46:15
I have a div with an attribute data-myval = "10" . I want to update its value; wouldn't it change if I use div.data('myval',20) ? Do I need to use div.attr('data-myval','20') only? Am I getting confused between HTML5 and jQuery? Please advise. Thanks! EDIT: Updated div.data('myval')=20 to div.data('myval',20) , but still the HTML is not updating. HTML <div id="mydiv" data-myval="10"></div> JS var a = $('#mydiv').data('myval'); //getter $('#mydiv').data('myval',20); //setter Demo Reference From the reference: jQuery itself uses the .data() method to save information under the names 'events' and

Unable to set data attribute using jQuery Data() API

落花浮王杯 提交于 2019-11-26 14:15:52
I've got the following field on an MVC view: @Html.TextBoxFor(model => model.Course.Title, new { data_helptext = "Old Text" })</span> In a seperate js file, I want to set the data-helptext attribute to a string value. Here's my code: alert($(targetField).data("helptext")); $(targetField).data("helptext", "Testing 123"); The alert() call works fine, it shows the text "Old Text" in an alert dialog. However, the call to set the data-helptext attribute to "Testing 123" does not work. "Old Text" is still the attribute's current value. Am I using the call to data() incorrectly? I've looked this up

jQuery: Select data attributes that aren&#39;t empty?

此生再无相见时 提交于 2019-11-26 12:05:10
问题 I\'m trying to select all elements that have a data-go-to attribute that is not empty. I\'ve tried $(\'[data-go-to!=\"\"]\') but oddly enough it seems to be selecting every single element on the page if I do that. 回答1: try $(':not([data-go-to=""])') UPDATE: For the sake of not leading anyone astray, this answer will work in older versions of jQuery but is not future-proof. Since @gmo and @siva's answers both seem to be working with later versions I defer to (and encourage you to upvote) their

Is there any problem with using HTML5&#39;s “data-*” attributes for older browsers?

牧云@^-^@ 提交于 2019-11-26 11:19:44
问题 I want to associate some custom data with some HTML nodes. I was going to use the new HTML5 style \'data-*\' attributes. e.g.: <tr class=\"foo\" data-typeid=\"7\">… , and then I was going to select this HTML node and show/hide it etc. by reading the value with $(node).attr(\"data-typeid\") . However this web page needs to work with older browsers aswell. I\'m not using the data-* attribute as a special attribute, but I\'d like to know if older browsers will ignore, wipe, or make inaccessible

Data attribute value updated by jquery is not visible in DOM

浪尽此生 提交于 2019-11-26 11:13:43
问题 I am updating a data attribute by jQuery, Like: jQuery(\'div\').data(\'hidden\', \'true\'); alert(jQuery(\'div\').data(\'hidden\')); Data attribute value got changed and returned new value which is true but DOM is still showing the old value which is false . 回答1: When you use .data() to update a data value, it is updating internal object managed by jQuery, so it will not be updated in the data-* attribute 回答2: I was beating around the bush so badly :( and able to solve the problem. Seams like

jQuery selectors on custom data attributes using HTML5

你。 提交于 2019-11-26 11:01:33
I would like to know what selectors are available for these data attributes that come with HTML5. Taking this piece of HTML as an example: <ul data-group="Companies"> <li data-company="Microsoft"></li> <li data-company="Google"></li> <li data-company ="Facebook"></li> </ul> Are there selectors to get: All elements with data-company="Microsoft" below "Companies" All elements with data-company!="Microsoft" below "Companies" In other cases is it possible to use other selectors like "contains, less than, greater than, etc...". $("ul[data-group='Companies'] li[data-company='Microsoft']") //Get all

Adding data attribute to DOM

自闭症网瘾萝莉.ら 提交于 2019-11-26 06:19:57
问题 $(\'div\').data(\'info\', 1); alert($(\'div\').data(\'info\')); //this works $(\'div[data-info=\"1\"]\').text(\'222\'); //but this don\'t work I\'m creating element within jquery. After that, I want to add attribute \"data\". He\'s like and is added, but in the DOM, this is not apparent, and I can\'t get the item, using $(\'div[data-example=\"example\"]\').html() jsfiddle 回答1: Use the .data() method: $('div').data('info', '222'); Note that this doesn't create an actual data-info attribute. If

Is there a limit to the length of HTML attributes?

荒凉一梦 提交于 2019-11-26 05:47:39
问题 How long is too long for an attribute value in HTML? I\'m using HTML5 style data attributes ( data-foo=\"bar\" ) in a new application, and in one place it would be really handy to store a fair whack of data (upwards of 100 characters). While I suspect that this amount is fine, it raises the question of how much is too much? 回答1: HTML 4 From an HTML 4 perspective, attributes are an SGML construct. Their limits are defined in the SGML Declaration of HTML 4: QUANTITY SGMLREF ATTCNT 60 --