JQuery Selectors for Dropdowns - javascript

this is not exactly a programming problem but more of a quest for more clarity on DOM manipulation using JQuery or Javascript. Any help will be appreciated.
i have a couple lines below i just need to understand how they are treated in the browser. the idea is just understand a little more
I know this line creates a reference to an Object, but what Object??
var child = $("select[Title='Country'], select[Title='Country Required Field'],select[Title='Country possible values']");
Does line empty the value property in this object? Why do you need this?
$(child).empty();
var options = <option value="volvo">Volvo</option>;
i know this is supposed to append the variable to the child object but what happens if there is already other values in the child object. What if you need to replace the values already in the child object with new the values in the option variable.
$(child).append(options);
the question is just for more clarity any answers will be welcomed thanks

Not sure if that what you mean but if you want to override the current options in the select fields by the options you've in the options variable without using empty you could use .html() :
$(child).html(options);
Hope this helps.

$(child).empty(); this line doesn't empty the value property of the object but it clears all the inner HTML of this object. This means it clears all the option tags inside the object. If you need to modify an existing value of an option with some other value you can do this:
`$("option[value='oldvalue']", child).val('new value');
The above code selects the option whose value you need to modify and then set its value to new value.

Related

How to delete an attribute when sending a partial object update to Algolia?

I need to use partialUpdateObject from the Algolia Javascript SDK to delete an attribute from an object that is already in the index.
My scenario is that I have a description field on my object that is optional. If the user originally sets a description and then later deletes it, I want to remove the description altogether from the object.
I do not want to overwrite the whole object (without the description attribute) because I have some count attributes on the object that I do not want to have to recalculate.
As far as I can tell in the documentation there isn't a way to do it and my workaround is to set the description as an empty string. Is that the recommended approach?
You're right: you cannot totally remove the attribute from an object with a partialUpdateObject operation. As a work-around you can set it to null, maybe that's enough for your use-case?
If you really want to delete the field you can :
Get your object with the search function
Store all fields values
Update (not partial update) your object without passing the field you want to delete

Adding values to object in js

So, I have the following js object structure:
I am trying to add a value to this, for example, "234324" after "ewerewr".
I tried obj["d7vi"] = new_value;
But it gets rid of all the previous values.
Any help?
make it
obj["d7vi"].push(newValue);
you need to add to the array rather than replace its existing values.

bind complex json object to knockout dropdown menu

I have a complex object being bound to a drop down. See the jsfiddle.
Is this the correct way to bind to a complex object for a drop down menu.
The drop down must bind to an initial value(currently working)
Changing the selected index in the drop down needs to update the knockout object. This is sort of working. The object is updated when save is called so the drop down's object value is being passed to the Format object. -- However.. This value is not updated in the UI.
I am not sure if it is the mapping that needs some work to make format into an observable. The value of SelectedFormat never seems to change from the first load.
any help on getting this to update the ui and text output of the object would be appreciated.
Edited: question to give requirements more clarity
code on js fiddle
1) No its easier than that, just use optionsText point out the member like
optionsText: 'Name'
Object reference is already implicit the optionsValue so you can skip that, if you want to explicit set it anway you can do the same there and just point to the member optionsValue: $data but its not needed. In other words, this would do http://jsfiddle.net/QrvJN/7/
2) The value and options binding are matched on object reference so if initial value and the list of options do not share references you need to match them yourself. You are doing it a little strange though, I did a binding for this that takes care of the problem http://jsfiddle.net/ewSU2/
3) THis is not needed if you check my solution 2)

How to dynamically create / destroy JS variables and append / remove their values in a string

I want to create javascript variables on the fly and append their values into a string. I actually am unable to find a method which dynamically declares variables in javascript. Once that is done I would want to do the reverse of this. So my question has two parts : one is related to creating and deleting variables on the fly as per requirement and the other is related to appending or deleting those variables value into a string at a specific point in the string.
There is the eval function.
But! you should ask yourself twice why do you need to "create variables on the fly"
eval on MDN
If I understand what you're after correctly, you should use an object for this:
var o = {};
o.name = "value"; // Creating a property
delete o.name; // Deleting a property
It sounds like you have a specific problem, could you perhaps share a bit more information about your problem?

How to get multiple "listKey" values from a Struts 2 <s:select>?

I have a code like this:
<s:select id="s" list="list" listKey="id" listValue="displayValue">
When I get the value from the object I'll always get the id.
How can I change the "listKey" value dinamically so I could get for example, the name of the object (supposing other of my attributes besides id is name ) instead always de id?
I was trying some code in jQuery like this:
function changeListKey(){
var id = $("#s").val();
$('#s').attr('listKey','name');
var name = $("#s").val();
document.write(name); // Only to test if I could get the value
}
When I execute it it doesn't seem to change the "lisKey" value so I could get another one, what would be the solution?
Thanks a lot in advance.
John Smith.
Why would you want to mix up what's used as the key value? That would make a mess on the server side.
In any case, listKey isn't an HTML attribute, it's a custom tag attribute, and is meaningless after the HTML has been rendered and sent to the client. You would need to iterate over the HTML select element's options collection and change the values on the options.
All this said, you'd be much better off doing any bizarre manipulations like this in the action itself, and exposing only the desired key/value pairs to the JSP, either in a list, or more easily, in a map. Using a map eliminates the need to provide listKey/listValue attributes--just use the map's key as the option text, and the value as the option's value.

Categories

Resources