0

Im trying to output jquery code in innerHTML function, here i provide simple example of problem, if i output var b than it works but a doesnt.

<p class="test">a</p>
<script>
document.getElementsByClassName("test")[0].innerHTML = myFunc(); 
function myFunc() {
var b = "hey";
var a = $(".test").text("F:\\PROD\\DATA\\Logglistor\\Logglistor " + new Date().getFullYear());
return a;
}
</script>
</body>
6
  • All the javascript links are right , i just didnt add them here! Commented Aug 3, 2018 at 14:26
  • Hi @Pikolinos, I don't understand what it is that you want to output. You want: "F:\\PROD\\DATA\\Logglistor\\Logglistor 2018" to be the output? Commented Aug 3, 2018 at 14:29
  • That's some very muddled code. For one thing you have syntax errors (always check the console.) Commented Aug 3, 2018 at 14:29
  • Yes i try to ouput :"F:\\PROD\\DATA\\Logglistor\\Logglistor 2018" Commented Aug 3, 2018 at 14:31
  • Then @Mamun answer is what you are looking for. Commented Aug 3, 2018 at 14:31

2 Answers 2

1

You must concat your string in .text()

Like that :

var a = $(".logglistPath").text("F:\\PROD\\DATA\\Logglistor\\Logglistor " + new Date().getFullYear())
Sign up to request clarification or add additional context in comments.

3 Comments

i made mistake writing it here , it doesnt help
Yes sorry i see it after. Why did you do $('.logglistPath) ? Because here, you get the element and you set the content. So after a is a jquery object, not a string
Ahah no probleme ! :)
0

You have to return text from the function and also you have to concatenate (+) the year with the other part of the string:

document.getElementsByClassName("test")[0].innerHTML = myFunc(); 
function myFunc() {
  var b = "hey";
  var a = "F:\\PROD\\DATA\\Logglistor\\Logglistor " + new Date().getFullYear();
  return a;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="test">a</p>

1 Comment

my problem was this line $(".test").text , i added twice to innerHTML and had [Object object] problem, thanks!

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.