0

JavaScript won't accept the code below, whats wrong?

<a href="#" <?php if ($tagOn) {echo 'id="tagOn"' } else {echo 'id="tagOff"'  } ?>
  onclick="addToSearch('xyz')">Xyz</a>

$tagOn is a Boolean.

1
  • 1
    You are missing semicolons after echo statements. Commented Nov 27, 2012 at 23:47

4 Answers 4

3

I think it's clearer if you define the variable first. It gets confusing if you put too much logic inline.

<?php $id = $tagOn ? "tagOn" : "tagOff"; ?>

<a href="#" "<?php echo $id ?> onclick="addToSearch('xyz')">Xyz</a>
Sign up to request clarification or add additional context in comments.

2 Comments

I don't see this setting the id
Thanks all - it was the semicolons :p
0

You're forgetting the semi-colons after each statement, try something like this:

<a href="#" <?php if ($tagOn) { echo 'id="tagOn"'; } else { echo 'id="tagOff"';  }  ?>
  onclick="addToSearch('xyz')">Xyz</a>

Comments

0

It might just be the missing semi-colons

<a href="#" <?php if ($tagOn) { echo 'id="tagOn"'; } else { echo 'id="tagOff"';  }  ?>
  onclick="addToSearch('xyz')">Xyz</a>

A cleaner way of doing this is with the ternary operator:

<a href="#" id=<?php echo ($tagOn) ? '"tagOn"' : '"tagOff"'; ?>
  onclick="addToSearch('xyz')">Xyz</a>

Comments

0

You forget semicolons !

<a href="#" <?php if ($tagOn) { echo 'id="tagOn"'; } else { echo 'id="tagOff"';  }  ?>
  onclick="addToSearch('xyz')">Xyz</a>

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.