Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

here is my script that runs from a CEWP. just want little search button, but it only works when i click the button, enter key just refreshes the page. I've tried the event.preventDefault() - but no luck - it's confusing.

i'm passed the point of no return. i must find a solution to this.

<script type="text/javascript">

function customSearch(inputId,type,site,scope) { 

var searchUrl = site + '/_layouts/OSSSearchResults.aspx?'; 

<!-- // Or '/_layouts/SearchResults.aspx?' -->

var searchTerm = '&k=' + document.getElementById(inputId).value;  var listParams = '&cs=This%20' + type + '&u=' + scope; window.location.href = searchUrl + searchTerm + listParams; 

} 

function searchKeyPress(buttonId,e) { 

<!-- // look for window.event in case event isn't passed in -->

if (window.event) {  e = window.event;  } 

if (e.keyCode == 13) {
    e.preventDefault();
    return false;
    document.getElementById(buttonId).click();  
    }
} 
</script>

<input name="searchBox1" id="searchBox1" onkeypress="searchKeyPress('searchButton1',event);" type="text"/> 

<input name="searchButton1" id="searchButton1" onclick="customSearch( 'searchBox1', 'Site', 'http://RegEng', 'http://RegEng' );" type="button" value="Search"/>
share|improve this question
    
did you also return false after the preventDefault? see stackoverflow.com/questions/895171/… –  Ryan Erickson Apr 5 '13 at 17:15
    
yep - my syntax must be bad somewhere. getting an "object doesn't support this prop/method in my debugger at the event.preventDefault. hmmmmm –  Bill Apr 5 '13 at 17:23
add comment

1 Answer

up vote 1 down vote accepted

Add a debugger in your function to make sure you are getting the event object correctly. Make sure buttonId and e are the objects that you expect.

Then you need to move the preventDefault(); return false to the end of the function because the return false will leave the function and nothing afterwards will get executed:

function searchKeyPress(buttonId,e) { 

    <!-- // look for window.event in case event isn't passed in -->

    if (window.event) {  e = window.event;  } 

    if (e.keyCode == 13) { 
        document.getElementById(buttonId).click();
        e.preventDefault ? e.preventDefault() : e.returnValue = false;
        return false;
    }
 }
share|improve this answer
    
rookie mistake there. thanks. still getting the error though. clearly i don't know what my event is. what do you mean by "add debugger"? –  Bill Apr 5 '13 at 18:00
    
I updated the answer, if you run that code it should prompt you to debug the code depending on what browser you are using. For IE hit F12 -> Script tab -> start debugging and refresh the page. –  Ryan Erickson Apr 5 '13 at 18:03
    
ok cool - so i can see my local vars and step through things - but honestly i'm not even sure what to look for. –  Bill Apr 5 '13 at 18:11
    
look at the e var, does it look like an event object? Does buttonId look like an event object? Are there any other parameters being passed in? You may have to change the parameters to your function –  Ryan Erickson Apr 5 '13 at 18:16
    
OK - so e is a DispCEventObj, and buttonId is a string. are there other parameters? no? yes? argh. –  Bill Apr 5 '13 at 18:24
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.