0

My function in HTML file is:

<p onclick='geturl('http://widefide.com/feed')'> Click Me! </p>

<p id="demo"></p>

Now what should be my javascript function to pass that url (i.e. http://widefide.com/feed) to "demo"? Like this? :

function geturl(ok)
{
   document.getElementById("demo").innerHTML = ok;
}

This is returning this error: "`Uncaught SyntaxError: Unexpected token } " Is this because thar

element is getting url from PHP+MYSQL? For instance:

print("<p onclick='geturl('" . $title["url"] . "')'>Click Me!</p>

2 Answers 2

1

Your first line should be (note the quotes):

<p onclick="geturl('http://widefide.com/feed')"> Click Me! </p>

And the JavaScript function should be:

function geturl(url){
   document.getElementById('demo').innerHTML = url;
}

function geturl(url){
       document.getElementById('demo').innerHTML = url;
    }
<p onclick="geturl('http://widefide.com/feed')"> Click Me! </p>

<p id="demo"></p>

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

8 Comments

Thanks. That worked :D Can I assign that value to x in this function? Like: function geturl(url) { x = url; } And them call that function in another function to get url? :)
If I understand what you're asking, then yes. You just need to define x outside of both functions first.
When I ran this code here, it worked. But in my code, it didn't and JavaScript console returned "Uncaught SyntaxError: Unexpected token } " :/ #DifferentStory
Sounds like a typo but without an example to look at I could only guess.
Added more code in my original post for reference :)
|
0

Better avoid inline event handlers, use addEventListener instead.

To write the url in #demo, you can use getElementById to get it, and then set the content using textContent.

var demo = document.getElementById('demo'),
    els = document.getElementsByClassName("geturl-trigger");
function geturl() {
  var url = this.dataset.url;
  demo.textContent = url;
}
for(var i=0; i<els.length; ++i) els[i].addEventListener('click', geturl);
<p class="geturl-trigger" data-url="http://widefide.com/feed">Click Me!</p>
<p class="geturl-trigger" data-url="http://example.com">Click Me!</p>
<p id="demo"></p>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.