0

I have this script in my < head > :

<script>
var rad = document.getElementsByName('search_type');
alert(rad[1]);
var prev = null;
for (var i = 0; i < rad.length; i++) {
    rad[i].onclick = function() {
        (prev)? console.log(prev.value):null;
        if(this !== prev) {
            prev = this;
        }
        console.log(this.value)
        alert(this.value);
    };
}
</script>

and this is my form:

<form name="search_form" action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
    <table border="0"> 
        <tr>
            <td class="input">
                <input type="radio" name="search_type" value="search_code" checked>1st type<br>
                <input type="radio" name="search_type" value="search_title">2nd type<br>
                <input type="radio" name="search_type" value="search_filter">3rd type<br>
            </td>
        <tr/>
    </table> 
</form>

but none of alerts work. I have no error in console. please help.

4 Answers 4

3

You are running the script before the HTML code for the radio buttons has been parsed.

Place the script below the form, or run the code in the load event:

window.onload = function() {

  var rad = document.getElementsByName('search_type');
  alert(rad[1]);
  var prev = null;
  for (var i = 0; i < rad.length; i++) {
    rad[i].onclick = function() {
      (prev)? console.log(prev.value):null;
      if(this !== prev) {
        prev = this;
      }
      console.log(this.value)
      alert(this.value);
    };
  }

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

6 Comments

Thanks! I placed it below the form and it worked fine. now please lead me to some well-documented StackOverflow page where people asked to control a second form elements ability-disability by this radio button values.
@MrCode: Yes, you are right, I corrected that. Too used to writing jQuery ready handlers... :/
but this has a problem yet. this and prev are always the same. what to do to capture real prev value?
@PedarJepeto: When I try it, they are different. As prev is initially null, it can't be the same as any element, and as prev is only assigned a value if it's different from this, it has to be different to be assigned anything at all.
but i see them exactly the same as I run the code. I dont know more about javascript. please help
|
1

You could simplify your code with some jQuery:

$(document).ready(function() {
    var rad = $('[name="search_type"]');
    console.log(rad);
    var prev = null;
    rad.click(function() {
        console.log(prev, $(this).is(':checked'));
        if (this !== prev) {
            prev = this;
        }
        console.log(prev, $(this).is(':checked'));
    });
});

http://jsfiddle.net/f2aDK/

1 Comment

thanks. thats the point. but this and prev got same values. what to do to capture the real prev value?
1

Move it script after form. Until Browser parse DOM element, javascript cannot read DOM.

<form name="search_form" action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
    <table border="0"> 
        <tr>
            <td class="input">
                <input type="radio" name="search_type" value="search_code" checked>1st type<br>
                <input type="radio" name="search_type" value="search_title">2nd type<br>
                <input type="radio" name="search_type" value="search_filter">3rd type<br>
            </td>
        <tr/>
    </table> 
</form>

<script>
var rad = document.getElementsByName('search_type');
alert(rad[1]);
var prev = null;
for (var i = 0; i < rad.length; i++) {
    rad[i].onclick = function() {
        (prev)? console.log(prev.value):null;
        if(this !== prev) {
            prev = this;
        }
        console.log(this.value)
        alert(this.value);
    };
}
</script>

1 Comment

thanks. thats the point. but this and prev got same values. what to do to capture the real prev value?
0

first add jquery lib

 $(document).ready(function () {
             $('[name="search_type"]').live('click', function (event) {
                     var rad = document.getElementsByName('search_type');
                        alert(rad[1]);
                       var prev = null;
                       for (var i = 0; i < rad.length; i++) {
                           rad[i].onclick = function() { 
                               (prev)? console.log(prev.value):null;
                               if(this !== prev) {
                                prev = this;
                                }
                              console.log(this.value)
                              alert(this.value);
                            };
                        }
    });
    });

Comments

Your Answer

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