I have an ID that has a class name such as...
<div id="nav" class="style">
I have an array containing all of my IDs called allIds. I'm trying to select an ID from it and then grab its class name. Here is what I have.
var grabClass = $("#"+allIds[0]).map(function()
{
return this.class;
});
I would expect var grabClass to be equal to style. However, if I console log grabClass it says...
[prevObject: b.fn.b.init[1], context: document]
Not exactly sure how to make grabClass equal to ID nav's class style.
Use the below to get class name
var grabClass = $("#"+allIds[0]).attr("class")
Please try this
$("#"+arr[0]).map(function(item){
console.info(this.className);
});
})();
We usually use map to iterate over a set of values in an array. However in this case if you iterate over set of arrays you will be getting the class of last id.
And the above approach is not suitable for iterating over multiple values.
You may want to have a look at this
http://jsbin.com/viyizohexa/edit?html,js,console,output
Hope this be of some help.
Happy Learning
Related
var onHomePageLoaded = function(retMsg)
{
$scope.data = retMsg.data.records;
$scope.data.link : 'http://www.newwebsite.com'
}
After i have added link element (key/value) to the javascript object, i am not able to get the same in the HTML template
<div ng-repeat="record in data">
<a ng-href="{{record.link}}"> Click Here </a>
</div>
Javascript is a dynamic language. You can add properties to existing objects in a very simple way , like assigning a value to an existing property. Just add a new property
$scope.data.link = 'http://www.newwebsite.com'
if retMsg.data.records is an array, still you can add a property to $scope.data.
if you want different link for every object in array then, do this.
$scope.data.forEach(function(obj){
obj.link = "your custom link" // write your logic here to produce different link.
});
If data is an array you can use
$scope.data.push(yourData);
for example
$scope.data.push({link : 'http://www.newwebsite.com'});
Or if you want to access the objects inside the array and add them a key value pair you can do as follow:
// add the link to the first entry
$scope.data[0].link = 'http://www.newwebsite.com';
Sorry. Do not know if I understood well.
Maybe you can define scope.data as:
$scope.data = {retMsg.data.records}
Then for example a function:
$scope.addNew = funtion(){
$scope.data.newElement = $scope.viewElement
};
In your HTML
<label>{{data}}</label> // Which makes reference to the $scope.data at the controller
<input ng-change="addNew()" ng-model="viewElement"></input>
<label>{{data.newElement}} // Will be empty at the very beginning but will show the new element once it is created.
Hope it helps
I see several issues with your code.
First, you use the variable name record in your ng-repeat, but then use report in ng-href. I assume those should be the same.
Also, link isn't a member of record, it is a member of data. You set it as a member of data here: $scope.data.link : 'http://www.newwebsite.com'. If you want to add that link to each record, in your onHomePageLoaded function, you'll need to loop through all the records you add to data, and add the link property to each one.
I have a list of list items that i get like this:
var searchResultItems = $(resultContainerId + ' li');
The search result items can have different classes. How can i get all items of a certain class from the searchResultItems? Im looking for something like:
var highPriorityItems = searchResultItems.find('.highPriority');
I know find() does not work in this situation because it goes through child elements. I am hoping there is something similar what im looking for.
The .filter method can do exactly what you want if substituted for .find in your example.
I have an unordered list with class name .summary_ul with a number of list items of class .summary_ul_li, each of which has a 'val' attribute whose value is a string of letters. I am calling this function from the javascript file:
function sort_by_val(){
var $tosort = $('.summary_ul_li');
$tosort.tsort({attr:"val"});
}
However, $tosort does not sort, and remains in its original order. I am trying to use this plugin:
http://tinysort.sjeiti.com/
Is there something I'm missing here?
I am trying to build an array containing some data-attributes that a div have. It looks like this.
<div class="myclass" data-test="hello"></div>
I've come this far, but not sure how to continue:
$('#container .myclass').each(function () { mArray. })
I have no idea what to with the array...
And the output I am looking for is
array 0
... test = hello...
or something like that.
I guess I could take all ITEMS by just writing them and adding, but it seems like there should be an easier way of doing so.
data() returns an object containing all the data attributes. Could that be something?
$('#container .myclass').data()
If your div has attributes beginning with the data-prefix (data-test="hello" data-info="some text.." and so on), you can store the values of all these attributes in an object with the jQuery data method, for example like this:
var myObject = $("div.myclass").data();
This is one possible solution , using map(): DEMO HERE
var dataTests =$('#container .myclass').map(function(){
return this.getAttribute('data-test');
}).get();
alert(dataTests[0]);//element 0
I have this code :
<script>
function getgroup(){
var i=0;
var total=document.getElementById("selectedOptions").length;
while (i<total)
{
var group=document.getElementById("selectedOptions").value;
var group2=group.substring(2);
alert(group2);
i++;
}
}
</Script>
I want to loop inside the list and get the value of each item in the list.
By using this code I am getting only the value of the first item only.
Any help please?
i think u are using getElementById() method and by specification the ids of elements should be unique so the length will always be one. try adding a class and use getElementsByClass()
You are assigning the value of the select element to the group variable. You need to loop through the options instead:
group = document.getElementById("selectedOptions").options[i].value;