javascript php post undefined -


in following snippet of javascript code, there times when html element scenario not present on php page. when javascript called, value entered in first code line below "undefined". scenario numeric input default value 0, tried check if or not posted html element integer, restore default value. not working. please guide how check , restore default value if html element not present on page. remaining code posts javascript variable php file, updates database , works fine when html element present on page. there different conditions because of html element may or may not present on php page.

var scenario = $("#scenario"+tournament_id).val(); if(!isnan(scenario)) {     scenario = 0; } 

check if element exists in dom .length or can use $("#scenario"+tournament_id).is(':visible') if element going be visible in page(if present).

  if($("#scenario"+tournament_id).length==0 || !$("#scenario"+tournament_id).length)     {        scenario = 0;        console.log("no element found");     } 

or:

if(!$("#scenario"+tournament_id).is(':visible'))  {       scenario = 0;       console.log("no element found");  } 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -