vbnullchar Posted June 12, 2007 Share Posted June 12, 2007 how will i know i know if an elementID already exist? Quote Link to comment Share on other sites More sharing options...
nogray Posted June 12, 2007 Share Posted June 12, 2007 Well, you can use a "try and catch" statment. It's not a perfect solution but it works. <script language="javascript"> function checkID(){ try { var objID = document.getElementById(document.getElementById('input1').value).id; alert("ID is already in use"); return true; } catch(e){ alert("There is no object with this ID"); return false; } } </script> <div id="div1">First Div</div> <input type="text" id="input1" value="div1" /> <button onclick="checkID();">Check if ID</button> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.