2

I want to run a function and also check if element1 is enabled and if so to disabled it and element2 and the other way around.

<input type="" onclick = "function(); element1.disabled==true ? (element1.disabled=false, element2.disabled=false ) : (element1.disabled=true, element2.disabled=true)" />

How do i make this code work ?

Since you can't used ";" inside "()" i tried "," but it didn't worked.

7
  • why don't you just call a function that can do the logic ?? declare a function in the same page and then call it : onclick="myFunction()" Commented May 20, 2015 at 9:52
  • if you declare a function you need to wrap the body with { } : function(){...}, your code should work Commented May 20, 2015 at 9:56
  • You also need to replace your semicolons with commas Commented May 20, 2015 at 10:06
  • @btx9000 I tried with commas, didnt work for me.. Commented May 20, 2015 at 10:14
  • @Hacketo can you please show me ? Commented May 20, 2015 at 10:15

3 Answers 3

2

In this case you can use chained assignment:

element1.disabled==true ? element1.disabled=element2.disabled=false : element1.disabled=element2.disabled=true
Sign up to request clarification or add additional context in comments.

Comments

2

There's no need for the ternary operator, you can just negate element1.disabled:

<input type="checkbox" onclick="element1.disabled = element2.disabled = !element1.disabled" />

and you can also chain the assignments (each assignment "returns" the value)

Comments

1

Remove "function()" you don't need that, and replace semicolons to commas. Your code should look like:

<input type="button" onclick = "element1.disabled==true ? (element1.disabled=false, element2.disabled=false) : (element1.disabled=true, element2.disabled=true)">

2 Comments

The function() means I also want to run a function in the onclick event along-side the complex if.
in that case <input type="button" onclick = "name_of_function(), element1.disabled==true ? (element1.disabled=false, element2.disabled=false) : (element1.disabled=true, element2.disabled=true)">

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.