Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,272 members, 7,811,799 topics. Date: Sunday, 28 April 2024 at 07:50 PM

See The Javascript Code That Is Turning My Brain - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / See The Javascript Code That Is Turning My Brain (2857 Views)

The Javascript Thread / Help On Html Code That Displays Form Details After Submitting Form / Help Me Keep My Brain In My Skull! Solve This Logic! (2) (3) (4)

(1) (2) (Reply) (Go Down)

Re: See The Javascript Code That Is Turning My Brain by lillylove2(f): 3:37pm On Jun 27, 2016
Locotech dueal ANTONINEUTRON sleepingdemon Joshuabase wisemania spikesC Booyakasha crotonite splashz hollyfat Kodejuice teampregar

Thanks everybody. It's now working. I really appreciate the help
Re: See The Javascript Code That Is Turning My Brain by michaelwilli(m): 5:51pm On Jun 27, 2016
lillylove2:
Please guys help me o. I've searched all over the internet and still can't find solution to this.

I have a link (html element), whose ID is automatically generated with PHP. Whenever the link is clicked, i want to insert the ID of the link into my database. The only problem now is how to get the ID.

This is my mock code:

///// The PHP that generates the id:
<?php
$id=(an array of numbers returned from a while loop);
?>


////the html link
<a href='#' onClick='MyId()' id='<?php echo $id ;?>'>Click Me</a>

/// the javascript function
function MyId() {
var ID=document.getElementById( the id of the element generated by php ).id;
}

My question now is how do i get and put the id of the clicked link into variable var ID
You can pass the id as an argument to the MyId function as below
<a href='#' onclick='MyId('<?php echo $id;?>') id='<?php echo $id;?>'>Click me</a>

Or u cn use jquery to retrieve d value of the id attribute
$('#id').attr('id')

U cn pm me if it doesn't work for you. Just willing to help

1 Like

Re: See The Javascript Code That Is Turning My Brain by sname00(m): 12:57pm On Jun 28, 2016
Joshuabase:
Lol passing a php variable to Javascript is fucking easy!

Inside the php file

<script>
Var id=<? Echo $id;?>;
Alert (id) ;
</script >

Php gets parsed first before any other language in a php file. That's why it's called "preprocessed hypertext programming language"

If your js is external, I only can help you with jquery, download the library and link it as you would link any js file.

In your php file

Do this

<link href="" id="<? Echo $id;? >" myattrib="<? Echo $id;? >" class="class">

In your js file

Var id = $(.class).attrib('myattrib');
Alert (id);

What this does is jquery selects the element with the class of 'class' (hope this isn't confusing)

Then checks for the attribute 'myattrib'

Then assigns the value inside myattrib to the variable 'id'

Hope it helps

PHP means ==> PHP: Hypertext Preprocessor, yeah I know funny. there's PHP inside PHP.. sort of an infinite loop.
Re: See The Javascript Code That Is Turning My Brain by danvery2k6(m): 11:13pm On Jun 28, 2016
Maybe You've gotten an answer, maybe not. But perhaps this may help someone else

HTML5 has this dandy attribute called data-* to which you can attach anything ex. data-id

You can add this attribute to any html tag and get its value in javascript.

Ex.

<a href ="#" id="someId" data-id="phpGeneratedId">Link Text</a>


Then in your Javascript, you can do this (I'm using JQuery here because I'm not good with Javascript)

$('#someId').on('click', function(){
var id = $(this).data('id');
doSomethingWith(id);
});
Re: See The Javascript Code That Is Turning My Brain by CodeHouse: 10:52am On Jun 29, 2016
Im just seeing this..page 1? Glad it is solved already!
Re: See The Javascript Code That Is Turning My Brain by lillylove2(f): 3:00pm On Jun 29, 2016
michaelwilli my email is not working o

(1) (2) (Reply)

6 Programming Exercises / I Teach You How To Build A Pure Sine Wave Inverter With Arduino Or (atmega328) / How Do I Use An Apache Server On Dreamweaver?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 17
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.