1

<legend>Information</legend>
<div class="row">
    <div class="col-xs-12 col-sm-3 col-md-3">
       <label><strong>{{$val}}</strong></label>
    </div>
    <script>
        var str = "Hello world!";
        var res = str.substring(0, 2);
        if (res == ee) {
            alert("Hello! I am an alert box!");
        } else if (res == he) {
            document.getElementById("checkbox").checked = true;
        }
    </script>
    <div class="col-xs-12 col-sm-3 col-md-3">
        <input type="checkbox" name="operator" id="checkbox" >
    </div>
    <div class="col-xs-12 col-sm-3 col-md-3">
        <input type="checkbox" name="operator" id="checkbox">
    </div>
    <div class="col-xs-12 col-sm-3 col-md-3">
        <input type="checkbox" name="operator" id="checkbox">
    </div>
</div>
										
<br/>

i have included my html piece of code above. i don't know why check box is not responding for this piece of JavaScript. note: i should use JavaScript in the position between div's

1
  • 5
    Please use class for this! Use unique id's! Never use same id's... Commented Nov 25, 2015 at 7:12

2 Answers 2

2

value ee and he should be wrapped in quotes. also note that IDs should be unique, you can rather use common class and use getElementsByClassName to target all of them using class :

var res = str.substring(0, 2);
if(res=='ee'){
  alert("Hello! I am an alert box!");
}
else if(res=='he'){
  document.getElementsByClassName("checkbox").checked = true;
}
Sign up to request clarification or add additional context in comments.

4 Comments

see this js fiddle jsfiddle.net/vcbwrg0L/1 still its not working
@VijayKumar: H should be capital in he or convert substring to lowercase and then compare. jsfiddle.net/peokog8m
@VijayKumar: check the fiddle which i shared. script should be there after dom element.
can i concatenate two strings for id of input fields like <input type="checkbox" name="operator" id="'11' + '00'" >
0

Condition is wrong. If you want check the string element you have to use "" or ''. Otherwise it will treat as a variable.

  if(res=="ee"){
     alert("Hello! I am an alert box!");
   }
   else if(res=="he"){
     document.getElementById("checkbox").checked = true;
   }

Comments

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.