1
function Open() {

  var cc = document.getElementById('FName');

  if ('Newfile.rtf' == cc.innerHTML) 
  {
     alert("New File");
  } //close If NewFile.rtf
  else {
     alert("Not new file");
  }
}//close Open()   

Here I have string "NewFile.rtf" in a element with id="FName" on the page. When the FName contains "Newfile.rtf" in it it stills goes to the else part of the function instead of going to if part. I tried different ways to write the compare statement in the if condition, no luck . Appreciate the help if anyone can help figure out this.

Thank you.

1
  • can we see your HTML portion of 'FName' please. Commented Dec 6, 2010 at 18:49

4 Answers 4

1

The simplest explanation is that your cc.innerHTML call is not returning what you think it is returning. Why don't you console.log or debug.

add something like

var innerhtml = cc.innerHTML;
console.log("innerHTML = " + innerhtml) // wont work in IE.

before the if statement.

Sign up to request clarification or add additional context in comments.

Comments

1

Try using regular expressions to find your filename, also check if the text you are searching is not into another DOM element, elimate left and right spaces, you should use Google Chrome for debuging the Javascript code:

var html = document.getElementById('FName').innerHTML; 
if( html.search("Newfile.rtf") != -1) { /*found*/ }
else { /*not found*/ }

2 Comments

what does the condition <> -1 means. This seems to be working . appreciate the help
@sony: the search() function returns the position of the searched string if found or -1 y not found. Operator might be !=, changed to reflect your observation.
0

but what's the type of this element? if it's about an input text type .. you can't use innerHTML but you'll use value then.

9 Comments

<li id="FName"> <%=FileName%> </li>
where FileName has "Newfile.rtf" string. I need this to work on IE
I wonder if we can define a li object as a var in javascript!
but if FileName="Newfile.rtf" string it will be : <%="Newfile.rtf"%>
I think the cc.innerHTML=="<%="Newfile.rtf"%>" not "Newfile.rtf"
|
0

Use innerText to get that

function Open() {

    var cc = document.getElementById('FName');

    if ('Newfile.rtf' == cc.innerText)
    {
        alert("New File");
    } //close If NewFile.rtf
    else {
        //enter code here
        alert("Not new file");
    }
}

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.