0

I'm very new to JavaScript so I apologize if this question has an extremely obvious answer. What I'm trying to do is pass the name of a text box in HTML to a function in Javascript via an onclick button. The goal of the function is to test a given string and highlight it based on certain parameters (for my testing, it is simply length).

There are multiple weird odds and ends within the functions that I'm aware of and working on, I know the functions work as when I remove the parameters and call the code text box directly, it prints exactly what I expect it to. But I want to be able to pass multiple text boxes without needing a specific function per box.

The code I have is as follows. I've included all of it in case the mistake was made somewhere I didn't expect it to be.

<!DOCTYPE html>
<html>
    <head>
        <style>
        .highlight {
        background-color: yellow;
        }
        </style>
    </head>
<body>

<label for="wordOne">Word One</label><br>
<input type="text" id="wordOne" name="wordOne"><br>
// Pass the value for the wordOne textbox to verify function
<button type="button" onclick="verify(wordOne,this)">Check</button><br><br>

<label for="wordTwo">Word Two</label><br>
<input type="text" id="wordTwo" name="wordTwo"><br>
// Pass the value for the wordTwo textbox to verify function
<button type="button" onclick="verify(wordTwo,this)">Check</button><br><br>
<p id="test"></p><br>
<p id="error"></p>

<script>
    // Highlights any code in a given line.
    function highlight(text,id,begin,end) {

        // document.getElementById("error").innerHTML = "TEST";

        var inputText = document.getElementById(id);
        var innerHTML = inputText.innerHTML;
        var index = innerHTML.indexOf(text)+begin;
        if (index >= 0) { 
            innerHTML = innerHTML.substring(0,index) + "<span class='highlight'>" + innerHTML.substring(index,index+text.length) + "</span>" + innerHTML.substring(index + text.length-end);
        inputText.innerHTML = innerHTML;
        return string;
        }
    }
    function verify(button,el){
        var begin=1;
        var end=1
        var id="test";
        var string = document.getElementById(button).value; 
        var len=string.length;
        if(len>5)
        {
            document.getElementById(id).innerHTML = string +" "+len;
            highlight(string,id,begin,end);  
        }
        else
        {
            document.getElementById(id).innerHTML = string;
        }
    }
</script>

</body>
</html> 

I apologize again if this is extremely obvious but I'm honestly not sure what I'm doing wrong. Thanks in advance for any help!

2
  • If I read your code correctly, all that's missing is that you write highlight('e','wordOne') instead of highlight('e') Commented Apr 15, 2020 at 14:36
  • @CherryDT Sorry, I honestly forgot I left that commented code in. It wasn't the issue. The idea is that a word will be typed into either of the text boxes and when the "submit" button is pressed, the contents of the respective text box will be passed to the verify function in script. Sorry for the confusion, I'll go through the code and add comments. I should've done that in the first place. Commented Apr 15, 2020 at 14:47

1 Answer 1

1

You can get the name of the textbox by the attribute

var x = document.getElementsByTagName("INPUT")[0].getAttribute("name"); 

And then use it in your function as

var x = document.getElementsByTagName("INPUT")[0].getAttribute("name"); 
 function highlight(x,id,begin,end) {

        // document.getElementById("error").innerHTML = "TEST";

        var inputText = document.getElementById(id);
        var innerHTML = inputText.innerHTML;
        var index = innerHTML.indexOf(text)+begin;
        if (index >= 0) { 
            innerHTML = innerHTML.substring(0,index) + "<span class='highlight'>" + innerHTML.substring(index,index+text.length) + "</span>" + innerHTML.substring(index + text.length-end);
        inputText.innerHTML = innerHTML;
        return string;
        }
    }

NOTE : By [0] it means the first one that is the first textbox.

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

2 Comments

You can also use var x = document.querySelector("input").name; if you prefer.
@Kunal I did, the site tells me that since my account is new, the upvote is "on record" but not visible. I tried multiple times.

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.