document.getElementById for PHP

Discussion in 'PHP' started by Autif, Oct 17, 2019.

  1. #1
    Hello,
    I’m more familiar with JavaScript than I am with PHP. That’s the reason for the framing in the title.

    I’m working on a WordPress site. As far as I can tell, it doesn’t really have any XML which I see several tutorials reference. So, what I would like to know is how to select an HTML element by its id and then echo the result without referencing any XML. Thank you.
     
    Autif, Oct 17, 2019 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,772
    Likes Received:
    4,523
    Best Answers:
    123
    Trophy Points:
    665
    #2
    That's not how php works - it just spits out text that happens to be html.

    You need to find the point in the template that generates the text that needs to change and edit it. You can't just tack code onto the end and expect it to change what's already been generated.
     
    sarahk, Oct 17, 2019 IP
  3. jstech2020

    jstech2020 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Try using "simplehtmldom" library, you can target elements like JS/jQuery
     
    jstech2020, Oct 24, 2019 IP
  4. Andrii Ozemko

    Andrii Ozemko Active Member

    Messages:
    79
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    58
    #4
    To be honest, I can not understand why document.getElementById is not suitable for you. It should find your element in the HTML. You don't need XML. Maybe, there is a mistake in your code.
     
    Andrii Ozemko, Oct 24, 2019 IP
  5. sbenjamin81

    sbenjamin81 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    The problem is that PHP is processed before the page is loaded. JavaScript functions can be called via events after the page has been loaded. Use PHP to build the HTML and JavaScript to manipulate it.
     
    sbenjamin81, Oct 24, 2019 IP
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    You need to read beginner tutorials to understand the languages first.
     
    NetStar, Nov 1, 2019 IP