0

I'm new to JQuery so if this is a obvious question my apologies. I have a simple form which holds some input fields. On the change-event I want to change a pre-defined array. The change event is triggered, but in this change-event I want to loop through all input-element again to fill/change an array. However the iteration doesn't work.

    <script>        
    jsonObj = [];
    $(document).ready(function(){

        $("input[class=domain]").change(function(){
            refreshData();                                                          
        });

        $("input[class=domain]").each(function() {
            var domain = $(this).attr("name");
            var score = $(this).val();

            item = {}
            item ["domain"] = domain;
            item ["score"] = score;

            jsonObj.push(item);
        });         
    });

    function refreshData() {
        alert("Text Changed");  <<-- This line is reached.      
        $(document)("input [class=domain]").each(function() {
            //TO DO: Refresh jsonObj 
            alert(domain); /<<-- This line is not reached.
        });         
    }
</script>    

A second question would be if it is possible to shorten this code. Now I have two separate function in the document.ready-event Change and each both on the input-element.

T.I.A.

3
  • Aren't you getting a TypeError: $(...) is not a function error in console? Commented Oct 8, 2015 at 11:26
  • 1
    You shouldn't have a space between input and [class=domain]. That will look for an element with class="domain" inside an input element. But input elements aren't containers, so they can't have any elements inside them. Commented Oct 8, 2015 at 11:38
  • So that should be input[class=domain] or just input.domain. Commented Oct 8, 2015 at 11:38

5 Answers 5

1
$('.domain').each(function(){
    alert(domain);
})

use this instead of $(document)("input [class=domain]").each

Sign up to request clarification or add additional context in comments.

Comments

1

You are missing a . and probably a .find before .each. Below code is what it should look like:

$(document).find("input[class=domain]").each(function() {
      //TO DO: Refresh jsonObj 
      alert(domain); 
}); 

UPDATE

With respect to your second question I would have shortened the code as below if the lines inside your .each was same as it would be in refreshData function:

jsonObj = [];
$(document).ready(function(){
    refreshData();//call once on DOM Load
    $('.domain').change(refreshData); //attach it as named function to change event
});

function refreshData() {
    //keep all this code here
    $(".domain").each(function() {
        var domain = $(this).attr("name");
        var score = $(this).val();

        item = {}
        item["domain"] = domain;
        item["score"] = score;

        jsonObj.push(item);
    });        
}

4 Comments

$(document).find()? RLY?
Why not..? It will work @VisioN .. I just sorted it out the way he was trying to achieve..
Sure. $(document).find("html body div form input[class=domain]") will probably also work. Why to write useless code?
Yea.. That's correct @VisioN Updated answer for his second question and this will achieve both the things..
1

I have done some rectification and you can shorten it like:

<script>        
    jsonObj = []; // array declaration
    $(document).ready(function(){
        $("input.domain").change(function(){ // <----better to use a class selector
            refreshData($("input.domain")); // <----pass the object in the function                                                         
        }).change(); //<----trigger it on load to execute       
    });

    function refreshData(el) { // get the passed object in the func arg
        $(el).each(function() { // <----have a loop on it this way
            var domain = $(this).attr("name"); // extract the name on current object in collection
            var score = this.value; // get the value of the current object in iteration
            var item = {}; // declare a new object
                item["domain"] = domain; // set new prop and assign value here
                item["score"] = score; // set new prop and assign value here
            jsonObj.push(item); // now push the object in the array.
        });         
    }
</script> 

1 Comment

Nice. I will try this. Thanks
1

This expression is wrong for some reasons:

$(document)("input [class=domain]")

A. There must be no space between input and [class=domain]. This is the difference between "input that has the class domain" (input[class=domain]) and "input that one of its sub-nodes has the class domain" (input [class=domain]).

B. In order to query inside a jQuery element you need to use the find method like this: $(document).find("input [class=domain]"). But because document is the root element, you can just write $("input [class=domain]").

P.S

In CSS-selectors (like jQuery) there is a special syntax for searching classes, so instead you can just write input.domain.

So this how the line should look at last: $("input.domain").each...

You can read more about css selectors here.

1 Comment

Thanks, I didn't notice the space between input and the bracket.
0

Strange code...

$(document)("input [class=domain]").each

Try this:

$("input[class=domain]").each

1 Comment

yes, my mistake. I removed (document) already, but it still doesn't work. Thanks for the reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.