Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have the following field,
<input id="department" value="finance"/>
I'm trying to create an object variable containing the field in plain old javascript like so.
var $field = $(document.getElementById("department"));
I'm then trying to pull the value from the field variable, however it's failing.
$field.value;
Does anybody know what I'm doing wrong?
That looks like a jQuery object, which is a collection of DOM elements, and the value property is attached to the DOM elements.
You should have:
var field = document.getElementById("department");
// now you can access field.value
jQuery objects have a val() method that gets you the value of that property:
var field = $('#department');
// here you can call field.val();
Lose the call to $() if you want a DOM element, and not a jQuery object:
$field = document.getElementById("department");
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I try to createElement as h1 and its attribute class, but i can not get it working.
here goes my post:
function krea_ta(){
var tagy = document.cteateElement("h1");
var clasy = createAttribute("class");
clasy.value = "myclass";
tagy.setAttributeNode(clasy);
tagy.innerText = "business";
}
Where is the problem?
First things first, you've spelled "create" wrong on "createElement".
Secondly, you can either set the class as an attribute using tagy.setAttribute("class", "myclass")
or via the classList property which expects an array of strings..
tagy.classList = ["myclass"];
Edit:
The above assign of the array might not be working on all browsers (works on Chrome), as MDN states that classList is a read-only property.
tagy.classList.add(["myclass"]);
The .add method accepts parameterised values, array of values and a single value.
Why not simply do it like this:
function krea_ta(){
const tagy = document.createElement("h1");
const tagyText = document.createTextNode("business");
tagyText.appendChild(tagyText);
tagy.classList.add("myClass");
document.body.appendChild(tagy);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have very annoying problem, I need to display MySQL queries by using a PHP foreach loop but because of this when i try to set a javascript value for each of the items that are looped through it only selects either the first or last value it ignores the rest.
Here's what i've tried so far:
I have given an input a class and given it a value as well, this is within the for-each loop.
<input class="classs" value="{{ $item->id }}"/>
Next i have tried to access this from a js function, which is outside of the for-each loop.
function addSubTaskToDatabase(){
var itemId = $('.class').val();
alert(itemId);
}
Problem is all this does is alert the last value in the for-each loop with that class, its the same for name and for id also.
Anyone know the fix..? all help is appreciated.
var itemId = $('.class').val();
is going to select all elements on the page with that class.
You probably want a click function on the elements, and use this to access the currently-clicked item:
$('.class').click(function() {
var itemId = $(this).val();
alert(itemId);
});
To get all values you need an $().each loop
$('.class').each(function() {
var itemId = $(this).val();
alert(itemId);
}
see https://api.jquery.com/each/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm new to JS and for an assignment I have to remove a div from a facebook post. I'm using
var el = document.getElementsByClassName("someclass");
to find the div, since FB doesn't use ID tags. I can get a value if i run the var but its not working with removeChild for some reason.
getElementsByClassName returns list of elements.
Let us say you have a class with name "class1" attached to multiple elements inside the document, it will return all the elements with class name "class1".
You can iterate though result of getElementsByClassName like normal array.
For example:
if you want to remove first element that matches with this class name. then
el[0].parentNode.removeChild(el[0]).
But i would suggest having better logic to remove this element.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two text Fields
I have two fields, which is reference and hotel id as stated on the image,
the value (valeur) is editable and the key (nom) is fixed, what I need to do is the above input field of parameters in JSON will be updated each time the below values (valeur) is changed
so input Parameters will get update just the values in JSON format which are modified below.
The input field parameters will be saved in the DB as JSON format like in the image
Maintain an object and on an event (say a submit button), pick the values of the textboxes and add to the object. Use JSON.stringify to add that to the hidden textbox.
Demo: http://jsfiddle.net/abhitalks/2hv2K/1/
var obj = {}; // maintain an object
$("#btn").on("click", function() { // on an event, say a button click
obj[$("#name").val()] = $("#value").val(); // add the values to the object
$("#result").val(JSON.stringify(obj)); // stringify and put in the hidden field
});
Edit (based on Op's comment):
If you want to update the object itself, change the handler accordingly:
Demo 2: http://jsfiddle.net/abhitalks/C63tW/2/
var obj = {};
$("input#name, input#value").on("change", function() { // handle change event
obj["reference"] = $("#name").val(); // update reference property
obj["hotel-id"] = $("#value").val(); // update hotel-id property
$("#result").val(JSON.stringify(obj)); // update the result field
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am curious to know, how can i assign multiple html elements to a single variable in jquery...
for an example if i am having...
$('.some').drags();
$('.some1').drags();
at one instance i want variable drgoff
var drgoff = $('.some').offset().top;
at other instance i want variable drgoff
var drgoff = $('.some1').offset().top;
i am using this drgoff in the function, so now my question is how can i get all that html elements in place of .some when that particular html element is called...
var drgoff is not inside function it is a global variable..
thanx for any help...
it can be done using if else also but that will be too lengthy..
Use jQuery's .add(). var drgoff = $('.some').add('.some1');
Live demo here (click).
Well don't know when you are setting the value for drgoff. If you have a function (as indicated in question), then you can pass it the class name for which you want to get the value like this:
function getDragOffValue(cname){
dragoff = $(cname).offset().top;
}
and use it like:
getDragOffValue(".name");
alert(dragoff);//gets value for elements with class .name
getDragOffValue(".name1");
alert(dragoff);//gets value for elements with class .name1
But I don't understand how your code will behave if you have multiple elements with same class name. It will return the offset().top value for first element in collection of elements with given class. In short $(".name").offset().top is as good as $(".name:first").offset().top.