how to access json data attribute - javascript

I have a data-attribute for several element that I'm using to store an ID and a value for each picklist values for each element. These will all be used to populate one field in a modal if one of the elements is edited.
I've added the ID and name for each option for each element into the data attribute like so:
{id:a0Dd000000RT1dOEAT,name:aa},{id:a0Dd000000RT1dPEAT,name:bb}, {id:a0Dd000000RT1dQEAT,name:cc},{id:a0Dd000000RT1dREAT,name:dd},{id:a0Dd000000RT1dSEAT,name:ee}
These represents 5 picklist options for one of the elements.
What is the syntax to extract each one? I've tried versions of:
$('#elementID').data('picklistvalues')[0].id
and its not working, any suggestions? I'm obviously far from a javascript expert.

You need to JSON.parse() the value that .data() returns; then you can work with the value as a full JavaScript object.
var data = $('#elementID').data('picklistvalues');
// the attr value in the OP is not quite valid JSON
var obj = JSON.parse('[' + data + ']');
var id = obj[0].id;

It looks to me that you need to place quotes around the "id" values. These are not quite numbers and will not likely parse as anything but strings.

Related

input value issues in javascript

I hav a google published csv sheet. I created a html table and want to display csv values from published csv sheet into all input fields inside html table.
due to a unspecified reason this question has been moved.
You can use Split() function https://www.w3schools.com/jsref/jsref_split.asp
let splitedResult = xmlhttp.responseText.split("\r");
You can assign the values to
element.value = splitedResult[0].trim();
element1.value = splitedResult[1].trim();
Check out this link for the implementation https://jsfiddle.net/SyamKumarD/xa49vspy/29/
I would suggest you to provide a iterable id to your table elements, so you don't need to hardcode the index to your split result and you can iterate through the element and assign the split value dynamically.

Can i save an html collection in one var using javascript?

i have the following html object : element
I just want to save the value in a variable. I tried using
var result = window.content.document.getElementsByClassName("glyphicon-ok")[0].getAttribute('value');
alert(result);
But return me null object. How can i save the element of this list in my var using javascript. The element is.
glyphicon.glyphicon-ok.positive-color
Important, check this image to check the html collection i'm trying save using javascript:
image
You can use querySelector to pass CSS selector inside and get the first element of the page selected by the CSS selector (if you want to get all the array, you can use querySelectorAll instead) like that:
let result = document.querySelector('glyphicon.glyphicon-ok.positive-color').value;
alert(result);
Edit: if it returns a null value, it's because your element doesn't have the attribute "value" set...

Getting Hash and Storing in Variable

I'm trying to take the hashed value from an object. What I'm basically doing is this:
target = $('a[href^="#products"]');
targetHashed = target.hash;
$targetHashed = $(targetHashed);
console.log(targetHashed);
I'm putting the reference in "target", then getting the hashing and everything following it with ".hash" then converting the variable that contains the hashed value "targetHashed" to an object so I can do things like getting the offset, etc. Problem is that "targetHashed" is outputting undefined whenever I try to append .hash to it. Anyone know where I'm going wrong?
If you want to get the value of the href attribute of the selected elements you need to use attr so your second line would look something like.
targetHashed = target.attr('href');
But that only selects the first element, if you want to use all of them then you'll need to loop through that array.

Post Hidden Input Element Values to Controller?

I have a number of hidden input elements on my ASP.NET MVC page. They each hold an integer value. They are identified by $('table#ratings input[name=newReviewRatings]').
Is it possible to post the integers in those hidden input elements using $.post() so that the controller is passed an array of integers?
This is probably easier using a <form> element and simply posting the form. But is it possible to do it using jQuery?
You should be able to get your array using something like this.
var data = [];
$('table#ratings input[name=newReviewRatings]').each(function(){
data.push($(this).val());
});
$.post(url, data);
An ideal fit for this situation is using map like:
var data = $('#ratings input[name=newReviewRatings]').map(function(){
return $(this).val();
}).get();
// Post the data to the respective url
$.post(your_url, data);
The .map() method is particularly useful for getting or setting the value of a collection of elements.

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