JQuery - Set "open" attribute without a value [duplicate] - html

How do I set a data attribute without adding a value in jQuery? I want this:
<body data-body>
I tried:
$('body').attr('data-body'); // this is a getter, not working
$('body').attr('data-body', null); // not adding anything
Everything else seems to add the second arguments as a string. Is it possible to just set an attribute without value?

The attr() function is also a setter function. You can just pass it an empty string.
$('body').attr('data-body','');
An empty string will simply create the attribute with no value.
<body data-body>
Reference - http://api.jquery.com/attr/#attr-attributeName-value
attr( attributeName , value )

Perhaps try:
var body = document.getElementsByTagName('body')[0];
body.setAttribute("data-body","");

The accepted answer doesn't create a name-only attribute anymore (as of September 2017).
You should use JQuery prop() method to create name-only attributes.
$(body).prop('data-body', true)

You can do it without jQuery!
Example:
document.querySelector('button').setAttribute('disabled', '');
<button>My disabled button!</button>
To set the value of a Boolean attribute, such as disabled, you can specify any value. An empty string or the name of the attribute are recommended values. All that matters is that if the attribute is present at all, regardless of its actual value, its value is considered to be true. The absence of the attribute means its value is false. By setting the value of the disabled attribute to the empty string (""), we are setting disabled to true, which results in the button being disabled.
From MDN Element.setAttribute()

Not sure if this is really beneficial or why I prefer this style but what I do (in vanilla js) is:
document.querySelector('#selector').toggleAttribute('data-something');
This will add the attribute in all lowercase without a value or remove it if it already exists on the element.
https://developer.mozilla.org/en-US/docs/Web/API/Element/toggleAttribute

simply try this, it will definately work....
document.querySelector("audio").setAttribute("autoplay", "");
this will showed like below code;-
<audio autoplay>
</audio>
if you wrote like,
$("audio").attr("autoplay", "");
then, this will showed like below code;-
<audio autoplay="autoplay">
</audio>

We have a lot of good answers here.
But, you have to see that inside Firefox it gives you data-body="" and inside Chrome it gives you data-body only.

Related

Disable close-on-click-modal in Element UI

With (Vue) Element UI I'm trying to disable a Dialog element from closing on click.
In the docs it say there is a close-on-click-modal attribute, but it's default is true and I have no idea on how to set it to false.
close-on-click-modal="false" gives this error: "closeOnClickModal". Expected Boolean, got String.
Is there a way to disable this setting?
Please try it by changing close-on-click-modal="false" to :close-on-click-modal="false"
If you use the double dot, the content of the attribute is evaluated as Javascript. If you don't, it's a string.

How to remove anchor tag '<a></a>' using javascript

How to remove anchor tag '' in java script?
When I inspected the page, below is the screenshot of what I got
Here is my code:
<div class="dropdownm1-content">
<b>SHOP ALL</b>
<b>SHOP BY CATEGORY</b>
<p class="mn_category">
Just get the Element by using the ID of it and then remove it with the remove() function. Like so:
var removeanchor = getElementById('YOURANCHORTAGID');
removeanchor.remove();
or without creating a variable:
getElementById('YOURANCHORTAGID').remove();
(replace YOURANCHORTAGID with the id of your anchortag). If you want to trigger this after an action just create a function and trigger it with the action you want :).
for further information check the mdn docs:
https://developer.mozilla.org/de/docs/Web/API/ChildNode/remove
You may should add some more information to your question for a more precise answer. However, for the time being this may helps you out.
If you try to use it, pay attention to the fact, that I only adressed the first Element with the class 'text_main' and only the first of its children with 'a' Tag. You may need to change this, according to your code.
// Removing a specified element without having to specify its parent node
container = document.getElementByClass("text_main")[0];
var node = document.getElementsByTagName("a")[0];
if (node.parentNode) {
node.parentNode.removeChild(node);
}
Further information:
https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild
For that specific link you showed:
document.querySelector('.dropdownm1-content .text-main a:first-child').remove()
Though I'd highly recommend curing the sickness, not the symptom.

why does my ng-init not working

<body ng-init="user=${userID};month=${month};curPageNum=${currentPage}">
I wrote this by JSP,and i initial the value in the body tag,
but in the controller,i wrote:
console.debug($scope.user + " "+$scope.month}
but only $scope.month have the value,$scope.user displays undefined,
and after some tries, i found that if the value contains letters,it just display undefine,only the pure number can work.
I don't know why this happen,so can you help me to solve this?Thank you very much
Because your initialisation of the variables does not wrap the values in strings they are failing to assign. the JS is reading it as user=mary; i.e. assign the variable mary to user. That's why numbers work. Try this :
<body ng-init="user='${userID}';month=${month};curPageNum=${currentPage}">
You can refer Dynamic ng-init variable - Angularjs
Also, You can refer http://www.w3schools.com/angular/angular_directives.asp
you can initialise value as below
<body ng-init="user='${userID}';month='${month}';curPageNum='${currentPage}'">

Any difference between .innerHTML and .set('html','') in mootools?

To set the html of elements on my site, I use mostly
$('elementId').innerHTML = "<p>text</p>";
Looking through the mootools docs, I found this example given:
$('myElement').set('html', '<div></div><p></p>');
Is there any difference between these? Should I go through and change .innerHTML to the mootools method, or doesn't it make a difference?
the reason why the first one works is because - as it stands - a $ selector (document.id) in mootools returns the actual element. this - in normal browsers - is identical to document.getElementById() and the element object exposes any and all of its attributes/properties for you to edit.
the problems with NOT using .set are:
when mootools 2.0 aka MILK gets released, it won't work as it will be wrapped like jQuery and the selector won't return the object (mootools is becoming AMD hence it won't modify native Types - Element, Array, Number, String, Function(maybe!) - prototypes).
you cannot chain this. with set you can: $('someid').set("html", "loading...").highlight();, for example.
set is overloaded - it can set either a single property or multiples by means of passing an object. eg, element.set({html: "hello", href: "#", events: boundObj});
look at https://github.com/mootools/mootools-core/blob/master/Source/Element/Element.js#L936-942 - you can pass an array as an argument and it will join it for you, this makes it easy to work with multi-line strings and ensures performance in IE
edit: the BBT fan has kind of opened a separate topic: should the framework try to block you / prevent you from doing things that break the browser?
if you want to, you can add disallowed elements by changing that setter Element.Properties.html.set = function() { var tag = this.get("tag"); ... check tag }; - isn't mootools great?
mootools - by default - will NOT try to prevent you from doing stupid shit [tm] - that's your responsibility :) try setting height on an element to a negative value in IE, for example. should the Fx class prevent you from doing that? No. Should the setter prevent you? No. The footprint of constant checks to see if you are not breaking means it will slow everything down in performance-critical cases like animations.

HTML/CSS form field with suggestion

What is it called or where can I find code for placing a 'suggestion' or grayed out text in a form field box that doesn't get pass as a value. I know i can prepopulate it, but want to use it to only provide guidance. Example, box that says " "
The terminology you're referring to is called a watermark.
There are many existing Javascript solutions written for this already, like this one.
JavaScript will do this. I've used the jQuery framework, for example:
Setting the value:
$('#comment_box').val('Optional comment..');
On click, removing the value:
$('#comment_box').val('');
On submit:
if (comment == 'Optional comment..'){
comment = '';
}
And submit your comment. I've left out the functions here but you can get an idea.
HTML5 has a placeholder attribute supported by many modern browsers.
(But alas not MSIE.)
The above-linked article explains how to test for support and implement a javascript fallback.
use
<input type=text disabled value='...'/>
(disabled wont pass the values, whereas readonly will pass the value)
I think what you are referring to is a watermark
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/TextBoxWatermark/TextBoxWatermark.aspx
or
there are jquery defaultvalue plugins