Jquery label value [duplicate] - javascript

This question already has answers here:
How to change label text?
(3 answers)
Closed 8 years ago.
I'm trying to set the value of label using Jquery. I've tried .HTML(), .Val(), and .innerHTML but nothing seems to work. Any help would be appreciated.
JavaScript
$("#txtObjective").val(responseData[0].objective);
HTML
<label class="puma_Label" id="txtObjective"></label>

Use textContent (.text() in jQuery). Only input elements have a value property.
$("#txtObjective").text(responseData[0].objective);
Mandatory vanilla explanation:
document.getElementById("txtObjective").textContent = responseData[0].objective;
As for the html options -- those are simply bad practice to use here. Unless you're rendering HTML, never use innerHTML or html().

Related

How to get the `html` element in Javascript? [duplicate]

This question already has answers here:
How to get the <html> tag HTML with JavaScript / jQuery?
(6 answers)
Closed 4 years ago.
I want to get the html element in Javascript. I can now use document.body.parentNode or document.getElementsByTagName("html"). But I think there should be a simple, "right" way to do it.
So, what's the right way to get the html element in Javascript?
I tried to use search engines, but I don't know how to tell them the html tag is different from any other HTML tag, so it yielded no result I wanted.
You can use:
document.documentElement
which points to the document's root html node.
https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement
Chek this answer
https://stackoverflow.com/a/22873490/3134112
reference:
https://developer.mozilla.org/en-US/docs/Web/API/Document.documentElement.
I'd say document.getElementsByTagName, querySelector etc. are all "right ways" to get reference to html tag, it doesn't get simpler than that.

How do I change the text in Javascript? [duplicate]

This question already has answers here:
How to change a text with jQuery
(8 answers)
Closed 6 years ago.
I have a javascript code. The code always appends some text to an old one. But I dont want that the text is attached to the old text. Instead, I want that it replaces the old text.
Here is the code line:
$("#werkstatt").append("Text");
How can I do that?
Thank you very much!
You have to use empty() method.
$("#werkstatt").empty().append("Text");
or simply you should use .text() method.
$("#werkstatt").text("Text");

Using :not() with attributes [duplicate]

This question already has answers here:
How can I get the elements without a particular attribute by jQuery
(7 answers)
Closed 9 years ago.
I'm trying to use jquery to grab all select elements which do not have the "multiple" attribute set. I tried with the :not() but I can't get it quite right.
$(document).ready(function() {
$('select:not(multiple)').select2();
});
You forgot the attribute selector. What you're looking for there is "a <select> that is not a <multiple>"
$("select:not([multiple])").select2();
$('select:not([multiple])').select2();
or
$('select').not('[multiple]').select2();

getting ruby value in jquery [duplicate]

This question already has answers here:
Getting Textarea Value with jQuery
(5 answers)
Closed 9 years ago.
I have a textarea in which I am populating a database record. I want to access the same value in textarea in jquery.
<textarea id="contact_address1"><%= #contact[:address1] %></textarea>
How do I do it? Pleasse help!
What you are looking for is jQuery's val() function:
$( "textarea#contact_address1").val();
Take a look: http://api.jquery.com/val/
Try
$("#contant_address1").text();
and let be sure that the id of your textarea should be "contant_address1".

How to get HTML element similar to getting BODY with document.body? [duplicate]

This question already has answers here:
How to get the entire document HTML as a string?
(16 answers)
Closed 5 years ago.
I know there's a way to do this, but I cannot recall how.
How do I go about grabbing the HTML element (top-most element in the DOM), using plain JavaScript?
Use document.documentElement.
See the docs: https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement
var html = document.getElementsByTagName('html')[0];

Categories