0

The is(:focus) was the aproach. The final code is listed below:

setInterval(function(){
    if($j("SELECT[name='cf20_field_7']").is(":focus")) return false; 
    var information = '';
    var i = 1;
    $j("#cf20_field_1").html();
    //add new information to hidden field
    $j("#cforms20form .info_for_email").each(function(){
        var name = $j(this).find("INPUT[name='cf20_field_5']").val();
        var inn = $j(this).find("INPUT[name='cf20_field_6']").val();
        var view = $j(this).find("SELECT[name='cf20_field_7']").val();
        //render
        information += i + ")";
        information += "Наименование организации: " + name + ".<br/>\n";
        information += "Реквизиты организации: " + inn + ".<br/>\n";
        information += "Стоимость заказа: выписка " + view + ".<br/>\n";
        i++;
    })
    $j("#cf20_field_1").html(information);

    hovered = true;
}
    ,100
);

Is there some possibility to fire function when there is no hover in SELECT field.

And also there may be aproach that to check is there is no hover on SELECT field.

It cause problemms. When you are trying to select another option cursor is begging while setInterval is working.

The best approach that i find is listed below:

//every 100 mil secconds update info
setInterval(function(){

    $j("SELECT[name='cf20_field_7']").trigger('change');
    if ( $j("SELECT[name='cf20_field_7']").on("change")) return false;
    var information = '';
    var i = 1;
    $j("#cf20_field_1").html();
    //add new information to hidden field
    $j("#cforms20form .info_for_email").each(function(){
        var name = $j(this).find("INPUT[name='cf20_field_5']").val();
        var inn = $j(this).find("INPUT[name='cf20_field_6']").val();
        var view = $j(this).find("SELECT[name='cf20_field_7']").attr("value");
        //render
        information += i + ")";
        information += "Наименование организации: " + name + ".<br/>\n";
        information += "Реквизиты организации: " + inn + ".<br/>\n";
        information += "Стоимость заказа: выписка " + view + ".<br/>\n";
        i++;
    })
    $j("#cf20_field_1").html(information);
}
    ,100
);

More information: I can discribe situation more. So i had a form. onsubmit event didn`t work because there is another event is attachet. So i deside to update value of first field of form every 100 milisecs. The value is containing all dynamictly created "selects and inputs". But when i try to change value of the select by mouse. The function is fired and function check value of select and cause mouse begging. So i need somehow to check if that select is hovered to prevent firing of the function.

timofeiMih
  • 109
  • 1
  • 9
  • Frankly speaking - it is not clear what is your goal. Can you explain it? Possibly you need http://api.jquery.com/trigger/ to run change event. – Viktor S. Jan 08 '13 at 10:26
  • I need somehow check is there no hover on SELECT field. If it is i need to return false of the function. – timofeiMih Jan 08 '13 at 11:57

2 Answers2

2

Invalid here:

if ( SELECT[name='cf20_field_7'].on("change"))

I guess you need this:

if ( $("SELECT[name='cf20_field_7']").on("change"))

But still, the above is invalid. You need some handler like:

$("SELECT[name='cf20_field_7']").on("change", function(){
    return false;
});
Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    already tryed that way. I think this "return false" is only working for function in caller. So its not working. And "on" event always return true, because it only check for event. – timofeiMih Jan 08 '13 at 10:29
0
if ($j("SELECT[name='cf20_field_7']").on("change")) return false

Not clear what should be checked here. I assume you want to run some function attached to onchange even of select. In that case you should use .trigger instead of .on. But in both cases return value will be jquery object (for chaining purposes) so basically your statement will always be true both with trigger and on If you want to test some value of select, you should do something like next:

if(someTestFunct($j("SELECT[name='cf20_field_7']"))) return false;
function someTestFunct(jObj) {
    //some other code?
    return jObj.val() == "some value to test";
}

Possibly some better approach may be used, but without more details it is hard to suggest something.

Viktor S.
  • 12,736
  • 1
  • 27
  • 52
  • I can discribe situation more. So i had a form. onsubmit event didn`t work because there is another event is attachet. So i deside to update value of first field of form every 100 milisecs. The value is containing all dynamictly created "selects and inputs". But when i try to change value of the select by mouse. The function is fired and function check value of select and cause mouse begging. So i need somehow to check if that select is hovered to prevent firing of the function. – timofeiMih Jan 08 '13 at 11:43
  • first - you can use focus/blur to stop/start your setInterval function. Second - there is no problem with having more than one even on submit. So if it works for you - no problem to use it. Also, not sure what is a condition to update value of first field, but using setInterval seems to be strange. Possibly you can create corresponding example on http://jsfiddle.net and share your link here to get help – Viktor S. Jan 08 '13 at 11:53
  • Then maybe you can help me to do make it work using .submit(). I know that this will be better aproach. But $(this).stopImmidiatePropagation and $(this).preventDefault() can`t stop form from submiting. Is there some way to do my .submit() event before another submit event? – timofeiMih Jan 08 '13 at 11:56
  • You should use event.stopPropagation and event.preventDefault: `$('form').submit(function(e){ e.preventDefault(); })` Event object is passed to handler function as a first parameter. In all event handler functions `this` points to object where event happened (caught? not sure...). In case of submit it will be a form and preventDefault is a method of event object, not a form itself. – Viktor S. Jan 08 '13 at 12:00
  • no it is not working. I think there is somewhere in another place same function with e.preventDefault(). Is there is some way to make my function primary? – timofeiMih Jan 08 '13 at 12:04
  • If you need to stop form from submit, than there is no difference if there is another e.preventDefault() and when it is called. So, basically that should not make any problems. About changing handlers order: http://stackoverflow.com/questions/2360655/jquery-event-handlers-always-execute-in-order-they-were-bound-any-way-around-t – Viktor S. Jan 08 '13 at 12:08
  • I don`t know why. But form is still submitting after e.preventDefault() in my function. – timofeiMih Jan 08 '13 at 12:13
  • Also - here is how you can avoid checking if select is hovered: http://jsfiddle.net/zPFAs/ (results are show in console, just press f12 in any browser and find console there, you may need to install firebug for FF) – Viktor S. Jan 08 '13 at 12:13
  • You may have some error there. Open developer tools (like mentioned above) and check if you have some messages in console. Looks like you have some JS error and e.prevenDefault() does not happen because of it – Viktor S. Jan 08 '13 at 12:14
  • Well... I can't help without seeing your submit handler code and how it is attached. You may show it on http://jsfiddle.net – Viktor S. Jan 08 '13 at 12:20
  • The thing is that this will work if do it from scracth. Because this is everyday job) But in that context its not. Maybe you can check it on http://bd-spb.ru/test_form/ this is a test form. The script name is script_for_inn_ver2.js. The form in russian. But i think you will get the point. – timofeiMih Jan 08 '13 at 12:25
  • Besides, I see nothing there. It just say - Nothing found and no form. – Viktor S. Jan 08 '13 at 12:28
  • Also, in your JS file I see `if($j("SELECT[name='cf20_field_7']").is(":hover")) ...` There is no :hover selector in jQuery. But there is :focus selector which you can use instead – Viktor S. Jan 08 '13 at 12:43
  • Sorry for that. So i made it public. http://bd-spb.ru/?page_id=7483&preview=true Try to change "Вид выписки" for some another value and then try to change that again. – timofeiMih Jan 08 '13 at 12:58